如何修复Laravel页面未找到错误?

时间:2019-08-01 08:55:08

标签: laravel

注册后,它将重定向到/ home。但我收到404错误。当我将路线设置为/ home时,我会收到302个连续路线错误。

RegisterController.php

use RegistersUsers;

protected $redirectTo = '/';

public function __construct()
{
    $this->middleware('guest');
}

Route.php

Auth::routes(['verify' => true]);

Route::get('/', 'HomeController@index')->name('home');

HomeController

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

2 个答案:

答案 0 :(得分:0)

如果您在HomeController中构建了一个中间件,该中间件要求用户登录,那么您得到的是正常的。

您应该拥有一个使用“访客”中间件的登录页面。

用户注册后自动登录,然后将其重定向到您的home路由。

Auth::login($user)
//or
Auth::loginUsingId($userId);

答案 1 :(得分:0)

删除来宾中间件。

public function __construct()
    {
        $this->middleware('guest');
    }

您的 HomeController 应该看起来像

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

您的 route.php 外观

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

RegisterController 应该看起来像

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

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

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * 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', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

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

确保您正在使用laravel身份验证。