laravelメモ

# Route
[教材：PHP with Laravel for beginners - Become a Master in Laravel](https://www.udemy.com/php-with-laravel-for-beginners-become-a-master-in-laravel/)

## パラメータ設定

```php3
Route::get('/post/{id}/{name}', function ($id, $name) {
    return "This is post number ".$id . " " .$name;
});
```
http://192.168.64.2/cms/public/post/2000/taro

```php
Route::get('admin/posts/example', array('as'=>'admin.home', function () {
    $url = route('admin.home');
    return "this url is " . $url;
}));
```
http://192.168.64.2/cms/public/admin/posts/example

画面に以下が表示される。

`tis is urlhttp://192.168.64.2/cms/public/admin/posts/example`


## route list コマンド
```sh
cms:php artisan route:list
+--------+----------+---------------------+------------+---------+------------+
| Domain | Method   | URI                 | Name       | Action  | Middleware |
+--------+----------+---------------------+------------+---------+------------+
|        | GET|HEAD | /                   |            | Closure | web        |
|        | GET|HEAD | about               |            | Closure | web        |
|        | GET|HEAD | admin/posts/example | admin.home | Closure | webst        |
|        | GET|HEAD | contact             |            | Closure | web        |
|        | GET|HEAD | post/{id}/{name}    |            | Closure | web        |
+--------+----------+---------------------+------------+---------+------------+
cms:
```
route:listには route.phpの内容が表示される

```php
Route::get('/', function () {
    return view('welcome');
});

Route::get('/about', function () {
    return "about";
});

Route::get('/contact', function () {
    return "about";
});

Route::get('/post/{id}/{name}', function ($id, $name) {
    return "This is post number".$id . " " .$name;
});

Route::get('admin/posts/example', array('as'=>'admin.home', function () {
    $url = route('admin.home');
    return "tis is url" . $url;
}));
```
## Route::resource

まとめてcontrollerのメソッドを定義できる

```php
Route::resource('posts', 'PostsController');
```

```sh
ec2-user:~/environment/blog $ php artisan route:list
+--------+-----------+-------------------+---------------+----------------------------------------------+------------+
| Domain | Method    | URI               | Name          | Action                                       | Middleware |
+--------+-----------+-------------------+---------------+----------------------------------------------+------------+
|        | GET|HEAD  | /                 |               | Closure                                      | web        |
|        | GET|HEAD  | api/user          |               | Closure                                      | api        |
|        |           |                   |               |                                              | auth:api   |
|        | GET|HEAD  | posts             | posts.index   | App\Http\Controllers\PostsController@index   | web        |
|        | POST      | posts             | posts.store   | App\Http\Controllers\PostsController@store   | web        |
|        | GET|HEAD  | posts/create      | posts.create  | App\Http\Controllers\PostsController@create  | web        |
|        | GET|HEAD  | posts/{post}      | posts.show    | App\Http\Controllers\PostsController@show    | web        |
|        | PUT|PATCH | posts/{post}      | posts.update  | App\Http\Controllers\PostsController@update  | web        |
|        | DELETE    | posts/{post}      | posts.destroy | App\Http\Controllers\PostsController@destroy | web        |
|        | GET|HEAD  | posts/{post}/edit | posts.edit    | App\Http\Controllers\PostsController@edit    | web        |
+--------+-----------+-------------------+---------------+----------------------------------------------+------------+
ec2-user:~/environment/blog $ 
```

# Controller

## コマンドラインでControllerを作成
```sh
php artisan make:controller PostsController
```

基本的なメソッドまで自動生成

```sh
php artisan make:controller --resource PostsController
```

## ルートでパラメータ渡し

ルート

```php
Route::get('/post/{id}', 'PostsController@index');
```

controller

```php
    public function index($id)
    {
        //
        return "its working number is " . $id;
    }
```

## Viewに情報を渡す。

```php
$id = 1;
return view('post')->with('id',$id);
```

```php
$id = 1;
return view('post')->compact('id');
```

```php
return view('admin.news.index', ['posts' => $posts, 'cond_title' => $cond_title]);
```