【DockerToolbox + CentOS + apache + Laravel6.0】でLaravelのauth画面を導入する

ご存じの通り、Laravel6.0以降、php artisan make:authコマンドが使えなくなったため、auth画面の導入に手間がかかるようになりました(コレなんでわざわざこんなデグレみたいなことしたんだろ)。加えてDocker上でやることでさらに独自の手順などが出てきたりしたので、他サイトにもありますが、自分なりの実装手順メモ。

(1) laravel/uiパッケージをcomposerから導入

#laravel6の場合
composer require laravel/ui 1.*

#laravel7の場合
composer require laravel/ui

(2) LOGIN機能に必要なファイルの作成

php artisan ui vue --auth

(3) LOGIN機能に必要なDBテーブルの作成

php artisan migrate

(4) FrontEndに必要なPackageをインストール

npm install

(5) publicフォルダ配下にcss/jsをビルド

npm run dev

この内つまづいたのは(4)のnpm installのところ。[ERR]が連発して前に進めない。Dockerのフォルダ同期機能の関係らしく、ホスト側のPCの方でnode.jsをインストールし、(Windows/Mac側の)同期しているプロジェクトディレクトリ内でnpm installをするとうまくいった。

あと、「npm run dev」は「public」フォルダがプロジェクトディレクトリ内にないとこけるので注意。

【Laravel】アクセスしてきたドメインによってルーティングをわける

例えばフロントのWebページと管理画面を一つのLaravelで管理するけど、ドメインが別々な場合。アプリケーションにアクセスしてきたドメインによってルーティングを分ける時のメモ。

(1) まずアクセスしてくるドメインを「.env」ファイルに書き込む

DOMAIN_FRONT=example-front.co.jp
DOMAIN_ADMIN=example-admin.co.jp

(2) 「config」配下に以下の内容で新規ファイル「domain.php」を作成

<?php

return [

    'front' => env('DOMAIN_FRONT', 'Laravel'),
    'admin' => env('DOMAIN_ADMIN', 'Laravel'),

];

(3) 「app/Providers/RouteServiceProvider.php」にフロントと管理画面(ADMIN)用のルートを追記

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapFrontRoutes();

        $this->mapAdminRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    /**
     * Define the "front" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapFrontRoutes()
    {
        Route::namespace($this->namespace)
             ->group(base_path('routes/front.php'));
    }

    /**
     * Define the "admin" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapAdminRoutes()
    {
        Route::namespace($this->namespace)
             ->group(base_path('routes/admin.php'));
    }
}

(4) 「routes/web.php」をコピーし、フロント・管理画面用のそれぞれのルートファイルを作成し、必要なルーティング情報を記入
フロントページ用:routes/front.php
・管理画面用:routes/admin.php

(5) また、「app/Http/Controllers」フォルダや「resources」フォルダもfront, adminなどでフォルダ分けするとわかりやすい

これで「example-front.co.jp」からアクセスがあった場合は「routes/front.php」を、「example-admin.co.jp」からアクセスがあった場合は「routes/admin.php」を見に行くようにできた。サーバー側のvhostsで分けてLaravelプロジェクトを何個も作成する無駄が省ける!お試しあれ~

【Laravel】publicフォルダをプロジェクトディレクトリの外に配置する

public内にあるcssやjsなどのassetをフロントエンドの開発チームが触る、またセキュリティ上の理由から「public」フォルダをプロジェクトディレクトリの外に出したい時の対応方法のメモ↓


(プロジェクトディレクトリを「src」という名前にした場合)
(1) 「public」フォルダを切り取り、「src」と同じディレクトリ内におく

(2) あとは「public」フォルダ内の「index.php」の以下のパスの部分を

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

以下のように変える。そのまんま。

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../src/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../src/bootstrap/app.php';

(3) 後はドキュメントルートを移動した先の「public」に向くように調整するだけ

以上。

【php】composer updateを戻したい時

composer updateした時に何だかよくわからんエラーが出るようになってどうしようもない!って時は、update前のcomposer.lockが残っているならそのファイルがある場所で「composer install」すれば元の設定に戻せる

【Laravel】View bladeでLaravelの変数をJavascriptに渡す方法

例)ControllerでView bladeファイル(sample.blade.php)に$paramを渡す

(SampleController.php)
public function index(Request $request){
      return view('contents.sample')->with('param' => ['one', 'two', 'three']);
}

bladeファイル側では受け取った$paramを以下のような形でjavascriptに引き渡せる

(sample.blade.php)
<script type="text/javascript">
    	var param = @json($param);
    	alert(param);
</script>

【Laravel】How to pass laravel variable to javascript in blade file

Say I passed $param to sample.blade.php in Controller

(SampleController.php)
public function index(Request $request){
      return view('contents.sample')->with('param' => ['one', 'two', 'three']);
}

Then in the blade file, all you have to do is just pass $param to js like this

(sample.blade.php)
<script type="text/javascript">
    	var param = @json($param);
    	alert(param);
</script>