のぐそんブログ

暗いおじさんがシコシコ書くブログです。

LaravelでのRouteの書き方のメモ

Routeの書き方

LaravelでのRouteの書き方のメモです。

基本

Route::get(アドレス,関数など)

viewファイルを使う http://localhost/hello/resources/views/home.blade.phpを表示する。

Route::get('hello',function(){
    return view('hello');
});

表示する文字列を使う

Route::get('hello',function(){
    return '<html><body><h1>hello</h1></body></html>';
});

ヒアドキュメントを使う

$hello = <<<EOF
<html>
<body>
<h1>hello</h1>
</body>
</html>
EOF;

Route::get('hello',function() use ($hello){
    return $hello;
});

Controllerを利用する

直接viewを表示するのではなく、controllerをよびだすこともできます。 通常はこちらの使い方のほうが多いと思います。

controllerのあとの@xxxにはcontroller内の関数を指定します。

Route::get('/hello','HelloController@index');

controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class HelloController extends Controller
{
    public function index()
    {
      view('hello);
    }
}

Routeパラメータを使う

アクセスする際に、アドレスに{パラメータ}とつけることで、パラメータを渡すことができます。

Route::get('/アドレス/{パラメータ}',関数など)

例) http://localhost/hello/hogehogeでアクセスする。

無名関数でパラメータを使う

Route::get('hello/{param}',function($param){
   return 'hello ' . $param;
});

//hello hogehogeと表示される

controllerに渡す

Route::get('/hello/{param}','HelloController@index');

controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class HelloController extends Controller
{
    public function index(compact('param'))
    {
      //なんか処理する
    }
}

任意パラメータにする パラメータはそのまま利用すると、必ずアクセスパスにつける必要があります。 パラメータの末尾に?をつけることで、任意のパラメータにすることができます。

Route::get('/hello/{param?}','HelloController@index');

RESTfulなルーティング

以下のコマンドでRESTに準拠したコントローラーを自動で作成できる。

php artisan make:controller RestappController --resource

以下が作成される。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RestappController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

routerの指定はRoute::resourceで指定する。

Route::resource('hello','RestappController');

全部のメソッドを利用しない場合は、ルーティングを制限する。

Route::resource('hello','RestappController', ['only' => ['index', 'create', 'edit', 'store', 'destroy']);
path action verb
/hello index GET
/hello/creaete create GET
/hello store POST
/hello/{param} show GET
/hello/{param}/edit edit GET
/hello/{param} update PUT / PATCH
/hello/{param} destroy DELETE

参考

Laravelで作るRESTなWebアプリ

LaravelでRoute::resourceを使うときに気をつけること