使用auth中间件时,它会将我重定向到主页

时间:2016-04-01 00:13:44

标签: php authentication laravel-5

我是laravel app开发的新手。当我使用auth middleware时,它适用于未注册的用户(重定向到登录页面)。但当登录用户要访问该页面时,它会重定向到主页(根目录)。

来自routes.php代码的路线下面的

Route::group(['middleware' => 'auth'], function () {
    Route::resource('/edit', 'userController@edit');
});

我的userController.php代码下面是

<?php
 namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use App\Http\Requests;
 use App\allUsers;
 class userController extends Controller
 {
     public function index(){
     }

     public function show($id){
     }

     public function edit(){
         return view('auth.user_edit');
     }
  }

我的authController代码下面是

<?php

 namespace App\Http\Controllers\Auth;

 use App\User;
 use Validator;
 use App\Http\Controllers\Controller;
 use Illuminate\Foundation\Auth\ThrottlesLogins;
 use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
 {
 /*
     |--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/dashboard';

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
}

请任何人帮助我。

2 个答案:

答案 0 :(得分:0)

您可以在AuthController中覆盖$ redirectTo变量:

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/dashboard';

<强>更新

尝试更改路线:

Route::group(['middleware' => 'auth'], function () {
    Route::get('edit', 'userController@edit');
});

Route :: resource会自动为您创建所有CRUD路径。

https://laravel.com/docs/5.1/controllers#restful-resource-controllers

答案 1 :(得分:0)

是问题解决了。刚改变了路线

Route::group(['middleware' => ['web', 'auth']], function () {
     Route::get('/edit', 'userController@edit');
});