qwerty
qwerty

Reputation: 5246

Laravel: Nesting controllers and making parent controller redirect to children?

This is my first project in Laravel, so play nice with me!

The goal is to create a CMS. Every page will have it's own "slug", so if i name a page This is a test, it's slug will be this-is-a-test. I want to be able to view that page by going to example.com/this-is-a-test.

To do that, i'm guessing i'll have to do something like:

Route::any('(:any)', 'view@index');

And create a controller called View with an index method. All good, right?

The problem is creating the admin area. I'm gonna have a few pages within the area, a few examples are Dashboard, Pages, Settings and Tools. Since all of those are sub-pages in the admin, i figured it would be appropriate to make them nested controllers, right? The only problem is, when i visit /admin, i want to show the dashboard (/admin/dashboard) directly. I would prefer just calling the dashboard controller instead of redirecting to /admin/dashboard from the admin controller. Is that possible?

So, to illustrate what i mean:

example.com/admin -> loads admin.dashboard
example.com/admin/dashboard -> also loads admin.dashboard

Here are all my routes:

Route::get('admin', array('as' => 'admin', 'use' => 'admin.dashboard@index'));
Route::get('admin/dashboard', array('as' => 'admin_dashboard', 'use' => 
Route::any('/', 'view@index'); // Also, should this be below or above the admin routes? This route will show the actual cms pages.'admin.dashboard@index'));

And here is my admin_dashboard controller:

class Admin_Dashboard_Controller extends Base_Controller {

    public $restful = true;    

    public function get_index()
    {
        return 'in dashboard';
    }

}

The view controller just displays a link to the admin page, that works. I just can't figure out what's wrong with the admin routes? When i go to /admin or admin/dashboard i just get a blank page, no 404. If i go to admin/blah or just blabla i get a 404, so i know something is happening, it's just not happening correctly. Am i missing something?

Upvotes: 2

Views: 2850

Answers (1)

qwerty
qwerty

Reputation: 5246

I had misunderstood the naming conventions for controllers.

This is how it was:

controllers
    admin
        admin_dashboard.php   Containing controller Admin_Dashboard_Controller

It's suppose to be:

controllers
    admin
        dashboard.php         Containing controller Admin_Dashboard_Controller

In other words, i shouldn't have prepended "admin" to the beginning of the controllers file name. It all works fine now.

I was actually able to minify the routing code to this as well:

Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' => 'admin.dashboard@index'));

Upvotes: 3

Related Questions