KondukterCRO KondukterCRO - 3 months ago 21
PHP Question

Define Laravel 5 route inside subfolder and display it via controller

I have Laravel 5.2.45 app.
I have controller structure like this:

App
Http
Controllers
Admin
AdminController.php


inside AdminController.php I have

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;

class AdminController extends Controller
{

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('is.admin');
}

public function index()
{
return view('admin.home');
}

}


I have views folder structure like this:

views
admin
home.blade.php


And inside my routes.php I have

Route::get('/admin/home', 'Admin\AdminController@index');


So I'm trying to get that when I type .../admin/home browser displays home.blade.php inside admin folder.

My routes.php:

Route::auth();

Route::get('/', 'FrontController@index');

Route::get('/home', 'FrontController@index');

Route::get('/add_user', 'FrontController@user');

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

Answer

The prefix is missing in your route definition. Correct it to look like this:

<?php
   Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
       Route::get('/home', 'AdminController@index');
   });

Now, try base_url/admin/home in your browser and it should work.