Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel slash after url redirects to root folder

I'm new to Laravel and it seems a great framework but i have a question when it comes to routing. I want to route all get requests to one controller action named PageController@view but when the prefix is admin i want it to route to Admin\AdminController@view. I have the following code:

Route::get('/{slug?}', 'PageController@view');

Route::group(array('prefix' => 'admin', 'namespace' => 'Admin', 'before' => 'auth'), function()
{
    Route::get('/', 'DashboardController@main');
});

But when i go to: /admin, Laravel treats it like a slug instead of a prefix. Is there somekind of except function for the first line or do i simply have to switch the above lines to get the right result?

New problem:

But now i have a new problem when i go to http://www.mysite.com/laravel/public/admin/, Laravel redirects me to http://www.mysite.com/admin/ instead of calling AdminController@main. When i remove the slash at the end like this: http://www.mysite.com/laravel/public/admin it works fine.

like image 329
user1507868 Avatar asked Feb 27 '14 08:02

user1507868


1 Answers

If you specify those routes in the opposite order it should work. Do your most specific routes first, and the more generic ones later. Think about it as a "catch this, this, and this, and then fall back to this generic one when none of the above match".


Your new problem lies in the (bad, IMO) redirect rule that Laravel 4.1 ships with. The rules very much assume that your Laravel public directory sits on the document root of the server. Here's the rule:

RewriteRule ^(.*)/$ /$1 [L,R=301

As you can see, that's saying "if the current URL ends in a slash, just redirect it to /{url}", thus getting rid of the slash. This is all well and good, just as long as your installation sits on your server's document root.

You can fix this by specifying a RewriteBase (in your case RewriteBase /laravel/public) above that rule in your public/.htaccess file, but of course, this will now change from environment to environment. This is why i consider the changing of Laravel 4.0's RedirectIftrailingSlash functionality to be a .htaccess rule to be a bad idea.

Alternatively, you can remove that RewriteRule entirely and try not to worry about the fact that your app will now respond on both URLs /laravel/public/admin and /laravel/public/admin/.

like image 175
alexrussell Avatar answered Nov 02 '22 10:11

alexrussell