Protecting routes in Laravel 4

0

After logging the user into the system I theoretically would have to have some protected routes, how do I do this? In my case it is returning error. See:

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 disengagement, my model.

ClientContact.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;
    }

}
    
asked by anonymous 09.06.2014 / 20:53

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
         ));
}
    
13.06.2014 / 11:49
0

Problem solved.

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

So, instead of trying to access by route, I can simply create a filter and call in 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. ;)

    
09.06.2014 / 22:14