Securing routes in Laravel 4

Asked

Viewed 343 times

0

after logging in the user system I would theoretically have to have some protected routes, how do I do that? In my case is returning error. Look at:

Routes.php

Route::group(['before' => 'auth'],function(){
    Route::controller('reserva','ReservaController');
});

Error:

ErrorException
call_user_func_array() expects parameter 1 to be a valid callback, class'Illuminate\Auth\Guard' does not have a method 'ClienteContato'

For the sake of conscience, my model.

Customerscontact.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class ClienteContato extends Eloquent implements UserInterface, RemindableInterface {

    public function __construct(){
        parent::__construct();
    }

    use UserTrait, RemindableTrait;
    protected $table        = 'cliente_contato';
    protected $primaryKey   = 'id_contato';
    protected $hidden       = array('password', 'remember_token');
    protected $fillable     = [...];
    public static $rules    = [];
    public $timestamps  = false;

    public function getAuthIdentifier(){
        return $this->getKey();
    }

    public function getAuthPassword(){
        return $this->password;
    }

}
  • Try to remove the __contruct of your model, or pass an array $attributes : public function __construct(array $attributes = array() { parent::__construct($attributes); }

2 answers

1


if(Auth::check()){
    Route::group(array('before' => 'auth'), function(){
        Route::get('/', array(
            'as' => 'home',
            'uses' => 'HomeController@index'// sua home do logado
        ));
    });
} else {
Route::get('/', array(
            'as' => 'home',
            'uses' => 'ContasController@getLogin'//sua tela de login
         ));
}
  • 2

    It’s nice to explain why your code solves the problem. Check out the guide [Answer].

0

Problem solved.

Regardless of the table name that will be used for user login, the class Auth will always have a method user(), which will allow access to the login table properties, which in my case is cliente_contato.

So instead of me trying route access, I can simply create a filter and call on my protected controllers, as I’ll show below:

Route::filter('logged', function(){
    if (Auth::check()) return Redirect::to('/');
});

And in all my controllers I load the filter in the constructor as follows:

$this->beforeFilter('logged');

Living and learning. ;)

  • you can mark your answer as accepted, so it is easy for other users to more easily identify the solution found to the question problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.