LoginSignup
8
11

More than 5 years have passed since last update.

LaravelでControllerの作成

Last updated at Posted at 2015-05-26

コントローラ作成コマンドを実行します。

php artisan make:controller コントローラ名

するとプロジェクト/app/controllers/以下に自分でつけた名前のコントローラファイルができているはずです。開いてみると、

コントローラ名.php
<?php
class コントローラ名 extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */

    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }


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


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


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }


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

という中身になっています。

それぞれの関数に目的があり目的によって呼び出すという感じになります。

これだけではControllerをよびだせないので、
routes.phpでつないであげる必要があります。
例えばPostControllerというのがあり
routes.phpで
Route::resource('post', 'PostController');
と書きます。

これでPostController内の関数を"post"という名前のURLとして呼び出すことができます。
たとえば、PostController内のindex関数を呼びたいなら~/public/post/indexです。

それではartisanコマンドで確認してみましょう。

php artisan routes

これでPostControllerへのパスが通っているのが確認できるはずです。

8
11
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
8
11