0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

laravelメモ

Last updated at Posted at 2019-06-12

Route

教材:PHP with Laravel for beginners - Become a Master in Laravel

パラメータ設定

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 "this url is " . $url;
}));

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

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

route list コマンド

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の内容が表示される

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のメソッドを定義できる

Route::resource('posts', 'PostsController');
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を作成

php artisan make:controller PostsController

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

php artisan make:controller --resource PostsController

ルートでパラメータ渡し

ルート

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

controller

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

Viewに情報を渡す。

$id = 1;
return view('post')->with('id',$id);
$id = 1;
return view('post')->compact('id');
return view('admin.news.index', ['posts' => $posts, 'cond_title' => $cond_title]);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?