php - Laravel Auth 不工作,一切似乎都是正确的

标签 php laravel laravel-4

下面是我的 Controller 代码...

    if(Auth::attempt($userdata)){

  //echo 'dfdf';
  //exit;
        return Redirect::to('mydata');
    }

如果我取消注释,它会显示 dfdf...所以这意味着 Auth 没问题...对吗??这是我的 routes.php 代码...

 Route::get('mydata', function()
 {
   return View::make('mydata');})->before('auth');

即使这样也行得通...如果我从 routes.php 中删除 ->before('auth'); 公共(public)函数 doLogin() {

    return Redirect::to('mydata');
    exit;
     }

这意味着连路由器都可以,对吧?

如果我删除 before('auth'); ,路由无需登录就可以工作,这似乎甚至路由都很好,那么 return Redirect::to('mydata '); 为什么它不重定向?即使在身份验证后它仍然停留在登录页面上...

最佳答案

评论后编辑:

在 Controller 中,将重定向更改为 ::intended:

if(Auth::attempt($userdata)){
    return Redirect::intended('mydata');
}

来自docs :

The Redirect::intended function will redirect the user to the URL they were trying to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.

API:

intended(string $default = '/', int $status = 302, array $headers = array(), bool $secure = null)

而且您不需要将 ->before('auth') 添加到您的 View 调用中,因为您正在重定向到 mydata Auth::attempt 返回true

因此,请将您的路线更改为:评论后编辑 感谢 Juan Antonio Orozco 指出此不足!!

Route::get('mydata', array('before' => 'auth', function() {
      return View::make('mydata');
});

顺便说一句,在 return 之后不需要添加 exit,因为解释器已经使用 return 命令离开了函数, 所以它甚至不会被执行。

关于php - Laravel Auth 不工作,一切似乎都是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23930604/

相关文章:

php - MYSQL:在地理空间查询中使用 VARCHAR 字段作为 POINT

php - 我如何获取文件的绝对路径

javascript - json.success 重定向到感谢页面

php - 删除 Sentry 帐户激活检查

php - 预测/纠正全文搜索

php - 使用 Laravel 显示类别和最低价格

JavaScript 点击函数表格单元格仅应用于第一行

php - PHP 中检查元素不包含特定 src 属性的 Xpath

php - 从 php 脚本执行 proc_open 以连接到 pgsql 时出错

php - mySQL 与 PHP 到 JSON 用于客户端图表