Laravel - 使用Eloquent登录auth

时间:2014-02-27 20:23:23

标签: php laravel laravel-4 eloquent

我的Laravel是全新安装。

routes.php文件

Route::get('/', 'MainController@idle');

Route::get('login', 'MainController@login');

Route::get('call', 'MainController@call')->before('auth');

MainController.php

class MainController extends Controller {

   public function idle() {
      return Response::json('idle');
   }

   public function login() {
      $user = array(
         'username' => Input::get('username'),
         'password' => Input::get('password')
      );

      if (Auth::attempt($user)) {
         return Redirect::intended();
      } else {
         return Redirect::route('login');
      }
   }

   public function call() {
      $users = User::all();
      return $users;
   }

}

filters.php

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});

请求:

http://l0calhost/survey/public/login

    InvalidArgumentException
Route [login] not defined.

为什么说它没有定义?

1 个答案:

答案 0 :(得分:1)

是的,因为您添加了AUTH过滤器

Route::get('call', array('before' => 'auth', 'uses' => 'MainController@call'));

Laravel会假设你有路线

Route::get('login', ...);

您可以在filters.php文件中找到类似的内容,然后将其更改为最适合您的内容:

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});

您还必须为自己的路线命名:

Route::get('login', array('as' = 'login', 'use' => 'MainController@login'));