4

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');
});
KondukterCRO
  • 543
  • 2
  • 16
  • 31
  • drop the leading forward slash so it becomes: `Route::get('admin/home', 'Admin\AdminController@index');` – Asher Sep 07 '16 at 08:23

3 Answers3

1

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.

Jonathon
  • 15,873
  • 11
  • 73
  • 92
  • I just did that and I get NotFoundHttpException in RouteCollection.php line 161: I typed into browser http://localhost:8888/project/public/admin/home Controllers and view are in correct folder as I mentioned in question I tried with return view admin.home, admin/home, home but nothing works – KondukterCRO Sep 07 '16 at 08:54
  • Can you post the contents of your routes file? – Jonathon Sep 07 '16 at 08:55
  • We both missed error - 'prefix' => 'admin' instead 'prefix', 'admin' But thanks for great suggestion – KondukterCRO Sep 07 '16 at 21:54
1

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.

Wdy Dev
  • 219
  • 2
  • 11
0

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

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

Asher
  • 557
  • 3
  • 18
  • This won't fix the issue, Laravel trims off any extra leading slashes when it registers the routes. – Chris White Sep 07 '16 at 08:50
  • Yes, this isn't working. Same problem as before. I tried every combination already. /admin/home, /home, home, admin/home, admin.home... – KondukterCRO Sep 07 '16 at 08:55