How to use Laravel's wildcards on route group before auth? - php

I have this code below in my router.php
Route::group(array('before' => 'auth'), function()
{
Route::get('account/(:all?)', function() {});
Route::get('facebook/(:all?)', function() {});
});
Route::controller(Controller::detect());
It works well when the user is not logged in. But once he is successfully logged in and gets redirected to the requested page, the page is not displaying anything; just a blank page. I have tried to use :any instead of :all and it does the same thing.
Can anybody identify the problems?

Your routes are mapped to empty closures. You need to return something or map them to controllers.
Route::get('account/(:any?)', function() {
return "Hello World";
});
Route::get('account/(:any?)', function() {
return View::make('accounts.index');
});
//assuming you have an AccountController.php
Route::get('account/(:any?)', 'account#index');
//automatically route all methods of a controller
Route::controller('account');
Check out the laravel docs on routing.

Apparently, I did not find the better solution for using the group filter. The way I do it now to redirect guests to auth is this:
Route::filter('before', function()
{
$open_routes = array(
'',
'home',
'auth',
'help'
);
if(!in_array(URI::segment(1), $open_routes) && Auth::guest()) {
return Redirect::to('/auth/login');
}
});

Related

Laravel 5 Auth Filter do not work

I just tried Laravel 5 after a time at 4.2..
The docs says it is possible to use 'before' => 'auth' as always, but for me it does not work.
I have no idea whats wrong, I have read the docs, search on internet but seems not to find anything.
My code looks as:
$router->group(['before' => 'auth'], function($router)
{
//
$router->get('admin', function()
{
return View::make('admin.index');
});
//
$router->get('login', function()
{
return View::make('admin.login');
});
});
Anyone can see what I doing wrong here?
In laravel5 filters are removed. Instead you can use middleware classes which are more clean.
In this blog you can read more about the middleware classes and that they're a replacement of filters.
If you want to do it with self written routes you can use this:
Route::group(['middleware' => 'auth'], function()
{
Route::get('admin', function()
{
return View::make('admin.index');
});
Route::->get('login', function()
{
return View::make('admin.login');
});
});

Laravel Route group change which view is used for guests and members

I'm currently trying to figure out the URL routing in Laravel, I have the basic pages working but I'm trying to figure out the best way to distinguish between guests and members.
The aim is to have the index page user splash view for guests and if a user is logged in then use the dashboard view.
Like this:
Route::group(array('before' => 'guest'), function()
{
Route::get('/', function()
{
return View::make('splash');
}));
});
Route::group(array('after' => 'auth'), function()
{
Route::get('/', function()
{
return View::make('dashboard');
}));
});
At the moment this doesn't work as it always seems to try and display the authenticated 'after' => 'auth' page even when a user is not logged in?
If you want to different views for the same route depending on whether logged in or not you just define the route once and in the closure check to see if the user is logged in and then return the relevant view
Route::get('/', function()
{
if (Auth::check())
return View::make('dashboard');
return View::make('splash');
}
Something like this may get you closer to the solution, but it will still not work, there is more work to do then just showing Views. Also, if you want to show different content on route '/' for auth users and guest users, you cannot use filter.
Route::group(array('before' => 'guest'), function()
{
Route::get('login', function()
{
return View::make('users.login');
}));
});
//~ Route::group(array('before' => 'auth'), function()
//~ {
Route::get('/', function()
{
// if user is logged in
if (Auth::check())
return View::make('dashboard');
else
return View::make('splash');
}));
//~ });

How to apply multiple filters on Laravel 4 route group?

Is it possible to add multiple filters on a group route in Laravel 4?
I have 2 authentification methods for an API centric application.
One with standard authentification (filter "auth" for website), one with token (filter "auth.token" for mobile app).
<?php
Route::group(array('prefix' => 'api/'), function() {
#Custom routes here
});
?>
Ideally I'd like that if one of the two filters pass, group is accessible.
You can:
Route::group(['before' => 'auth|csrf'], function()
{
//
});
However if you want to make it accesible if either of the filters passes, you'd have to write a little bit more (in filters.php):
function csrfFilter()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
}
function authFilter()
{
if (Auth::guest()) return Redirect::guest('login');
}
Route::filter('csrf-or-auth', function ()
{
$value = call_user_func('csrfFilter');
if ($value) return $value;
else return call_user_func('authFilter');
});
In routes.php
Route::group(['before' => 'csrf-or-auth'], function()
{
//
});
Remember you have to return nothing when the filter passes.
I hope this helps you!
You can do that with laravel
Route::group(array('prefix' => 'api/', 'before' => 'filter1|filter2'), function()
{
Route::get('api1', function()
{
// Has Filter1 and filter2
});
Route::get('api2', function()
{
// Has filter1 and filter2
});
});
check the documentation for more details

Prioritizing Laravel 4 routes

I have routes like this:
Route::group(array('before' => 'installed'), function() {
Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
{
Route::group(array('prefix' => 'gag'), function() {
Route::get('/', 'Admin\\GagController#index');
Route::get('delete/{id}','Admin\\GagController#delete');
});
});
});
I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.
if(App::environment() === 'demo')
{
Route::get('admin/gag/delete/{id}', function() {
die("You can't delete anything on demo application.");
});
}
//Actual routes are at the below.
However, it doesn't work when Route::get('delete/{id}','Admin\\GagController#delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)
Looks like routes.php parses my if block after routes are parsed.
How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.
Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.
Route filters are better to do this sort of restrictions:
Route::filter('checkDemo', function()
{
if (App::environment() === 'demo')
{
return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
}
});
And set the filter to your route:
Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function()
{
...
});
Or you can filter just that particular route:
Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController#delete'));

Laravel grouping routes with one name

i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me
I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.
Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.

Categories