KondukterCRO
KondukterCRO

Reputation: 543

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');
});

Upvotes: 4

Views: 1365

Answers (3)

Jonathon
Jonathon

Reputation: 16283

You can use route groups with the namespace and prefix options.

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

Here, the prefix allows you to specify the beginning of a URL that should always be in the routes inside the group. So any routes you put inside that group should start with admin.

The namespace lets you specifiy a folder/namespace for the controllers you reference. So all the controllers must be in the App\Http\Controllers\Admin namespace and the app/Http/Controllers/Admin folder.

Upvotes: 1

Wdy Dev
Wdy Dev

Reputation: 229

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.

Upvotes: 1

Asher
Asher

Reputation: 577

You need to drop the leading forward slash so it becomes:

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

Upvotes: 0

Related Questions