Php Laravel 4.2-如何仅允许登录/授权用户访问
我已经创建了登录表单,并使用Auth方法使用电子邮件和密码授权用户。登录是通过HomeControler中的doLogin函数完成的Php Laravel 4.2-如何仅允许登录/授权用户访问,php,laravel-4,laravel-routing,Php,Laravel 4,Laravel Routing,我已经创建了登录表单,并使用Auth方法使用电子邮件和密码授权用户。登录是通过HomeControler中的doLogin函数完成的 public function doLogin() { $rules = array( 'email' => 'required|email', // make sure the email is an actual email 'password' => 'require
public function doLogin()
{
$rules = array(
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('login')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
if (Auth::attempt($userdata)) {
$id = Auth::id();
return Redirect::to('panel');
} else {
return Redirect::to('login');
}
}
}
在我的routes.php文件中,我创建了通过id获取用户的路由,在此之前检查用户是否登录
Route::get('panel/{id}', array('before' => 'auth', function($id)
{
$user=User::find($id);
dd($user);
}));
到目前为止,我面临两个问题:
–我如何使用doLogin函数中的get参数创建路由,以便在panel/id/address中重定向用户?
return Redirect::to'panel'->带有'id',$id;这不管用
第二个问题是panel/{id}路由正在检查用户是否使用'before'=>'auth'登录,但如果我使用用户id 1登录,然后打开地址panel/2/它将打开当前路由。我如何让laravel检查登录mit的用户id是否打开当前路由。如何使laravel检查登录的用户id是否与路由中请求的用户id匹配?是否跟踪路由中请求的用户id
只需从BaseController扩展所有要授权的控制器 BaseController可能是
class BaseController extends Controller {
public function __construct() {
$this->beforeFilter('auth');
}
}
那就别管它了,它很好用。看到这个了吗