首页 > 解决方案 > 为什么我的网络登录转到 api.php 路由而不是 web.php

问题描述

我是 Laravel 的新手,我在 laravel 上有项目,但没有 api,它的登录在路由中使用 web.php 可以正常工作,在另一个项目中,当我添加护照 api 时,我的 web 登录重定向到 api.php 路由而不是 web 路由。我在不同的 auth.php 和提供程序文件中做了一些必要的更改来配置护照身份验证

路由 web.php

Route::get('/', function () {
    if(Auth::check()){return Redirect::to('home');}
    return view('auth.login');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home.show');
Route::get('/company/{CompanyId}', 'CompanyController@index')->name('company.show');

路由 Api.php

 Route::group(['middleware' => ['api'],['cors', 'json.response']], function () {
    
    // ...

    // public routes
    Route::post('/login', 'Auth\ApiAuthController@Login')->name('login.api');
    Route::post('/register','Auth\ApiAuthController@Register')->name('register.api');
    Route::post('/forgotpassword', 'Auth\ApiAuthController@ForgotPassword')->name('forgotpassword.api');
    

    // ...
});

登录刀片表单重定向

 <form method="POST" action="{{ route('login') }}">

中间件 authanticate.php 所做的更改

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

应用服务商

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
         'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();
        Passport::routes();
        
        //
    }

RouteServiceProvider.php

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('routes/api.php'));
    }
}

标签: laravellaravel-routinglaravel-passportlaravel-7

解决方案


因为$this->mapApiRoutes();在上面$this->mapWebRoutes();并且您的 URL(例如 /login)与Auth::routes();路由名称相同,请参阅以下内容以了解基本身份验证的工作原理:

// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');

// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');

推荐阅读