【问题标题】:Laravel 4 route protected pagesLaravel 4 路由保护页面
【发布时间】:2016-04-15 12:51:18
【问题描述】:

我有一个控制器UserController.php,其中包含以下方法: getIndex()、getAll()、getCreate()、postStore()、getShow()、getEdit()、putUpdate()、getDestroy()、getLogin()、getDashboard() 和 getLogout()。

我已在 routes.php 中包含以下代码

    Route::get('/', function()
    {
    return View::make('hello');
    });
    Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController@getAll']);
    Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController@getCreate']);
    Route::get('users/{all}/edit', 'UserController@getEdit');
    Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController@putUpdate']);
    Route::controller('users', 'UserController'); 

我可以访问像

这样的页面
http://localhost/testlaravell/users/

http://localhost/testlaravell/users/add

等等

现在,我希望只有登录的用户才能访问这些页面,否则他/她将被重定向到登录页面http://localhost/testlaravell/login

UserController.php下的登录方法如下:

public function postSignin() {

        $rules = array(
        'username'    => 'required', // make sure the username is an actual username
        'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
        );

        // run the validation rules on the inputs from the form
        $validator = Validator::make(Input::all(), $rules);

        // if the validator fails, redirect back to the form
        if ($validator->fails()) {
        return Redirect::to('users/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 {

        // create our user data for the authentication
        $userdata = array(
        'username'     => Input::get('username'),
        'password'  => Input::get('password')
        );

        // attempt to do the login
        if (Auth::attempt($userdata)) {


         return Redirect::to('users/dashboard')->with('message', 'Welcome User');

        } else {        

        // validation not successful, send back to form 
        return Redirect::to('users/login')->with('message', 'Sorry!! Username/ Password seems wrong.');

          }

     }       
 }

public function getLogin() {
    return View::make('users.login');
    }

【问题讨论】:

标签: laravel laravel-4 laravel-routing


【解决方案1】:

你会想要在 Laravel 4 中使用 Auth 过滤器

您可以将所有路由包装在一个组中并指定该过滤器,在您的情况下,代码将是这样的。

Route::group(array('before' => 'auth'), function()
{
    Route::get('users/all/', [ 'as' => 'users.index', 'uses' => 'UserController@getAll']);
    Route::get('users/create/', [ 'as' => 'users.getCreate', 'uses' => 'UserController@getCreate']);
    Route::get('users/{all}/edit', 'UserController@getEdit');
    Route::put('users/update/{id}', [ 'as' => 'users.putUpdate', 'uses' => 'UserController@putUpdate']);
    Route::controller('users', 'UserController'); 
});

您可以查看有关路由组here的文档

【讨论】:

  • 当我添加你上面建议的代码时,它显示 - “这个网页有一个重定向循环”。
  • 你能发布你的整个routes.php 一定是导致循环的东西。如果您可以在浏览器中使用检查器来查看当您尝试以未经授权的用户身份访问页面时它重定向到的内容,这也可能会很好。
  • 请检查上面的 routes.php 下的编辑代码
  • 我看到该页面在访问仪表板页面时已登录,但是,这显示为 - “此网页有重定向循环”
  • 我假设您的登录页面已经正常工作了?在 Laravel 5 中你必须绑定 auth Route 我忘记了它在 4 中是如何工作的。
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 2023-03-28
  • 2013-07-04
  • 2020-11-21
  • 2015-03-11
  • 2013-12-11
  • 2018-06-28
  • 2017-02-11
相关资源
最近更新 更多