I'm trying to route the index page to a different location if logged in however even though my authentication system works, it's not redirecting to where I expected i.e. getLogged, instead it always redirects to getIndex whether I am logged in or not.
Route::filter('auth', function()
{
if (!Sentry::check()) return Redirect::to('/');
});
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
Route::get('/', array('before' => 'detectLang', 'uses' => 'MyController#getIndex'));
I tested to make sure my auth works by changing
Route::group(array('before' => 'auth'), function() {
Route::get('/', array('uses' => 'MyController#getLogged'));
});
to
Route::group(array('before' => 'auth'), function() {
Route::get('/dash', array('uses' => 'MyController#getLogged'));
});
and that properly behaves that I can only access /dash when I am logged in so why is my index route not working?
You're declaring the same route twice, it won't work. To achieve this functionality, instead of adding a auth filter, add a guest one that, instead of checking if the user is not connected, will check if it is. Something like this:
Route::filter('guest', function () {
if (Sentry::check()) return Redirect::route('logged');
});
Then, setup your routes, something along these lines:
Route::get('/', array(
'as' => 'home',
'uses' => 'MyController#getIndex',
'before' => 'guest'
));
Route::get('/logged', array(
'as' => 'logged',
'uses' => 'MyController#getLogged',
'before' => 'auth|detectLang'
));
Note: The as key gives a name to your route, so you can use it on Redirect::route or URL::route methods.
Related
What is the best and correct way to write the following routing:
Route::group(['middleware' => ['web']],function (){
Route::prefix('user')->group(function () {
//this address shows the login page
Route::any('login', 'User#login_page');
//others address that control the login action
Route::prefix('login')->group(function (){
Route::get('google', 'User#check_user_login_with_google');
Route::post('form', 'User#check_user_login_with_form');
Route::get('google-url', 'User#redirect_to_google_url');
});
//these address control the registration actions
Route::any('register','User#register');
Route::any('register/check','User#check_user_registration');
});
});
You can do it all in one shot:
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
You can use all these in a separate associated array and assign this array to the group here is how to do that .
Route::group(['prefix' => 'user', 'as' => 'user.', 'middleware' => ['web']], function() {
Route::any('login', 'User#login_page');
...
})
Hope this may help you
I am updating a site from Laravel 4 to 5. In L4 I had this set up:
if(Sentry::check()){
Route::get('/', array('as' => 'school.home.index', 'uses' => 'school\AuthSchoolController#index'));
else{
Route::get('/', 'school\SchoolController#index');
}
Note the same url but different controllers depending on login or not.
With L5 I cannot use the middleware tried this:
Route::get('/', 'SchoolController#index');
Route::group(['middleware' => 'auth'], function()
{
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
});
But this just passes over the first and goes to the group, where it gets redirected to the login page and to the admin if logged in.
So I think I need an if/else equivalent in the route based on login but Auth::user() for doesn't seem to work:
if(Auth::check()){
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
}
else{
Route::get('/', 'SchoolController#index');
}
Route::get('/', function()
{
if( Auth::check() ) {
return app()->make('App\Http\Controllers\SchoolController')->callAction('index', []);
} else {
return app()->make('App\Http\Controllers\AuthSchoolController')->callAction('index', []);
}
});
Try reordering the routes like so:
Route::group(['middleware' => 'auth'], function()
{
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
});
Route::get('/', 'SchoolController#index');
I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code
In LoginController#doLogin
if (Auth::attempt($credentials)) {
return Redirect::intended("home.index");
}
In Routes
Route::group(array('before' => 'auth'), function() {
Route::resource('home', 'HomeController');
Route::get('/', 'HomeController');
});
My routes
If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below
Route::resource('home', 'HomeController');
Route::group(array('before' => 'auth'), function() {
Route::get('/', 'HomeController');
}); /* No Problem with this code */
But I want to work with auth filter.I'm using laravel 4.
Please help me...
Currently I use this:
in filter.php
Route::filter('sentry', function() {
if (!Sentry::check())
{
return Redirect::route('authLogin');
}
});
so in route.php
Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::group(array('before' => 'sentry'), function() {
Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index'));
Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index'));
});
I preffer named routes.
How to you add a filter to a route and pass a controller to it?.
In Laravel's doc they said that you can add a filter to a route like this:
Route::get('/', array('before' => 'auth', function()
{
return 'Not Authorized';
}));
But I need to pass a controller, like this:
Route::get('/', array('before' => 'auth', 'HomeController#index'));
But I get this error when I do it like that:
call_user_func_array() expects parameter 1 to be a valid callback, no array or string given
Any idea?
You should pass the controller function with uses key, So replace,
Route::get('/', array('before' => 'auth', 'HomeController#index'));
With,
Route::get('/', array('as' => 'home', 'before' => 'auth', 'uses' => 'HomeController#index'));
And there should be a route for login to process the auth filter like this.
Route::get('login', function()
{
if(Auth::user()) {
return Redirect::to('/');
}
return View::make('login');
});
Wanted to add another solution to your problem.
You can also use this, which in my opinion feels more readable.
Route::get('/', 'HomeController#index')->before('auth');
You only need to use "as" and "uses" if you're in need of named routes, eg. for a Form route.
I'm new to Laravel ( version 3 ), i do not know how to set Route and filters in Laravel so that any unauthorized user that is trying to access any url redirects to the login page (NOT the 404 error), in another word the default home page for unauthorized users is going to be the login page and for the authorized users it's going to be the dashboard.
If you are using laravel Auth class you can create an authorized route group. All routes that are defined there will be redirected if the user isn't logged in. Your router file will look something like this:
Route::get('/', array('as' => 'intro', 'uses' => 'intro#index'));
Route::get( 'login', array('as' => 'login', 'uses' => 'user#login'));
Route::get( 'logout', array('as' => 'logout', 'uses' => 'user#logout'));
// PROTECTED
Route::group(array('before' => 'auth'), function()
{
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'user#dashboard'));
});
// AUTH FILTER
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('login');
});
Just put a before filter in the declaration of the route like this
Route::get('edit_profile', array('before' => 'auth', function()
{
return View::make('profile.edit');
}));
The Auth filter exists by default in Laravel.