Laravel5 導入メモ


# プロジェクトの作成
 * ``` laravel new larablog ```
 
     or
 * ``` composer create-project laravel/laravel larablog --prefer-dist ```
 
# routes
 
``` php:routes/web.php
    Route::get('/', function () {
            return view('welcome');
    });
    
    Route::get('admin','AdminController@index');
    Route::get('article','ArticleController@index');
    
``` 
# IDE関連

```
K:\larablog>composer require barryvdh/laravel-ide-helper
```


```php:config/app.php
    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
// ...
        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class, // <- 追加
    ],
```

```
K:\larablog>php artisan ide-helper:generate
```
#Auth
```
K:\larablog>php artisan make:auth
Authentication scaffolding generated successfully.
```
```
K:\larablog>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
```
ログイン後のリダイレクト先を変更（以下4カ所）

 * app/Http/Controllers/Auth/LoginController.php
 * app/Http/Controllers/Auth/RegisterController.php
 * app/Http/Controllers/Auth/ResetPasswordController.php
 * app/Http/Controllers/Auth/VerificationController.php

```php
// ...
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin';  // <- ここを変更
// ...
```

#コントローラ
```
K:\larablog>php artisan make:controller AdminController
Controller created successfully.
```

#モデル
```
K:\larablog>php artisan make:model Article
Model created successfully.
```

#npm
先にNode.js をインストールしておくこと

``` npm install ```

``` npm run dev ```

## dataTables を追加してみる 
https://datatables.net/download/npm

``` npm install datatables.net ```

``` npm install datatables.net-dt ```

resouces/js/app.js に

``` 
require('datatables.net');
``` 

resources/sass/app.scss に

```
@import url('https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css');
```

最後に

``` npm run prod ```


#DebugBar

```
K:\larablog>composer require barryvdh/laravel-debugbar
```


```php:config/app.php
    'providers' => [
// ...
       Barryvdh\Debugbar\ServiceProvider::class,  // <- 追加
// ...    ],

    'aliases' => [
// ...
       'Debugbar' => Barryvdh\Debugbar\Facade::class,  // <- 追加
// ...
    ]
```

```
K:\larablog>php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
Copied File [\vendor\barryvdh\laravel-debugbar\config\debugbar.php] To [\config\debugbar.php]
Publishing complete.
```

.env に

```php
DEBUGBAR_ENABLED=null
```

こんな感じでメッセージング

```php
    public function index(Request $request)
    {
        $articles = Article::all();
        \Debugbar::debug("読みました");
        return view("article.index", ["articles" => $articles]);
    }
```

#サービスプロバイダー
```
mkdir app/Facades
mkdir app/Libraries
```
```
K:\larablog>php artisan make:provider LoggingServiceProvider
Provider created successfully.
```

```php:app/Probiders/LoggingServiceProvider.php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class LoggingServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(
            'logger',
            'App\Libraries\Logger'
        );
    }
}
```

```php:app/Facades/Logger.php
<?php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Logger extends Facade
{
    protected static function getFacadeAccessor(){
        return 'logger';
    }

}

```

```php:app/Libraries/Logger.php
<?php

namespace App\Libraries;

use Log;

class Logger
{
    private static function logging($message) {
        return print_r($message, true);
    }

    public static function debug($message, array $context = array())
    {
        Log::debug(self::logging($message), $context);
    }

    public static function info($message, array $context = array())
    {
        Log::info(self::logging($message), $context);
    }
// ...
}

```

```php:config/app.php
'providers'=>[
//...
         App\Providers\LoggingServiceProvider::class,
//...
]

'aliases'=>[
//...
        'Logger' => App\Facades\Logger::class,
//...
]
```