LoginSignup
2
1

More than 5 years have passed since last update.

LaravelでController以下にサブディレクトリを作る

Posted at

Laravelでアプリケーション開発をしていて、管理画面側のルーティングを作る時に少し詰まったので方法を記録しておこうと思います。

今回はApp\Http\Controllers以下にAdminというネームスペースで管理画面へのルーティングを作成します。

PHP:バージョン7.1.14
Laravel:バージョン5.6

ルーティングを追加する。

routes/admin.php

<?php

Route::group(['namespace' => 'Admin'], function() {
    Route::get('/', 'AdminController@index');
});

RouteServiceProviderに追加する.

app/Provider/RouteServiceProvider.php

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapAdminRoutes();
    }

    /**
     * Define the "admin" route for application
     *
     * @return void
     */
    protected function mapAdminRoutes()
    {
        Route::prefix('admin')
            ->middleware('admin')
            ->namespace($this->namespace)
            ->group(base_path('routes/admin.php'));
    }

Controllerを作成する。

app/Http/Controllers/Admin/AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function index()
    {
        return view('admin/index');
    }
}

viewを作成する

resources/views/admin/top.blade.php

@extends('layouts.app')

@section('content')
<div>
    hoge
</div>
@endsection

こんな感じでできるはずです。
あとで追記するかもしれません。

2
1
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
2
1