php 路由 [登录] 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29303783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:20:47  来源:igfitidea点击:

Route [login] not defined

phplaravel

提问by inkd

Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit localhost/project/public:

今天第一次尝试使用 Laravel。当我尝试访问 localhost/project/public 时出现以下错误:

InvalidArgumentException
Route [login] not defined.

InvalidArgumentException
路由 [登录] 未定义。

app/routes.php:

应用程序/routes.php:

<?php

Route::get('/', 'HomeController@redirect');
Route::get('login', 'LoginController@show');
Route::post('login', 'LoginController@do');
Route::get('dashboard', 'DashboardController@show');

app/controllers/HomeController.php:

应用程序/控制器/HomeController.php:

<?php

class HomeController extends Controller {

    public function redirect()
    {
        if (Auth::check()) 
            return Redirect::route('dashboard');

        return Redirect::route('login');
    }

}

app/controllers/LoginContoller.php:

应用程序/控制器/LoginContoller.php:

<?php

class LoginController extends Controller {

    public function show()
    {
        if (Auth::check()) 
            return Redirect::route('dashboard');

        return View::make('login');
    }

    public function do()
    {
        // do login
    }

}

app/controllers/DashboardController.php:

应用程序/控制器/DashboardController.php:

<?php

class DashboardController extends Controller {

    public function show()
    {
        if (Auth::guest()) 
            return Redirect::route('login');

        return View::make('dashboard');
    }

}

Why am I getting this error?

为什么我收到这个错误?

回答by Jeff Lambert

You're trying to redirect to a named routewhose name is login, but you have no routes with that name:

您正在尝试重定向到名称为的命名路由login,但您没有具有该名称的路由:

Route::post('login', [ 'as' => 'login', 'uses' => 'LoginController@do']);

The 'as'portion of the second parameter defines the name of the route. The first string parameter defines its route.

'as'第二个参数的部分定义了路由的名称。第一个字符串参数定义了它的路由

回答by aisthetes

In app\Exceptions\Handler.php

在 app\Exceptions\Handler.php

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(route('auth.login'));
}

回答by jhon chacolla

Try to add this at Header of your request: Accept=application/jsonpostman or insomnia add header

尝试在您的请求的标题中添加此内容:Accept=application/json邮递员或失眠添加标题

回答by Dibyendu Mitra Roy

You need to add the following line to your web.php routes file:

您需要将以下行添加到您的 web.php 路由文件中:

Auth::routes();

In case you have custom auth routes, make sure you /login route has 'as' => 'login'

如果您有自定义身份验证路由,请确保 /login 路由具有 'as' => 'login'

回答by thangavel .R

Laravel has introduced Named Routesin Laravel 4.2.

Laravel在 Laravel 4.2 中引入了命名路由

WHAT IS NAMED ROUTES?

Named Routes allows you to give names to your router path. Hence using the name we can call the routes in required file.

什么是命名路线?

命名路由允许您为路由器路径命名。因此,使用名称我们可以在所需文件中调用路由。



HOW TO CREATE NAMED ROUTES?

Named Routes created in two different way : asand name()

如何创建命名路线?

以两种不同方式创建的命名路由:asname()

METHOD 1:

方法一:

Route::get('about',array('as'=>'about-as',function()
    {
            return view('about');
     }
));

METHOD 2:

方法二:

 Route::get('about',function()
{
 return view('about');
})->name('about-as');

How we use in views?

我们如何在视图中使用?

<a href="{{ URL::route("about-as") }}">about-as</a>

Hence laravel 'middleware'=>'auth'has already predefined for redirect as login page if user has not yet logged in.Hence we should use askeyword

因此,如果用户尚未登录,laravel 'middleware'=>'auth'已经预定义重定向为登录页面。因此我们应该使用as关键字

    Route::get('login',array('as'=>'login',function(){
    return view('login');
}));

回答by Nirav Chavda

In case of API , or let say while implementing JWT . JWT middleware throws this exception when it couldn't find the token and will try to redirect to the log in route. Since it couldn't find any log in route specified it throws this exception . You can change the route in "app\Exceptions\Handler.php"

在 API 的情况下,或者在实现 JWT 时说。JWT 中间件在找不到令牌时会抛出此异常,并将尝试重定向到登录路由。由于在指定的路由中找不到任何日志,因此会引发此异常。您可以在“app\Exceptions\Handler.php”中更改路由

use Illuminate\Auth\AuthenticationException;

使用 Illuminate\Auth\AuthenticationException;

protected function unauthenticated($request, AuthenticationException $exception){
        return $request->expectsJson()
             ? response()->json(['message' => $exception->getMessage()], 401)
            : redirect()->guest(route('ROUTENAME'));
}

回答by Delino

Try this method:

试试这个方法:

look for this file

寻找这个文件

"RedirectifAuthenticated.php"

“重定向ifAuthenticated.php”

update the following as you would prefer

根据您的意愿更新以下内容

 if (Auth::guard($guard)->check()) {
   return redirect('/');
 }

$guard as an arg will take in the name of the custom guard you have set eg. "admin" then it should be like this.

$guard 作为 arg 将采用您设置的自定义守卫的名称,例如。“admin”那么它应该是这样的。

if (Auth::guard('admin')->check()) {
  return redirect('/admin/dashboard');
}else{
  return redirect('/admin/login');
}

回答by clone45

I ran into this error recently after using Laravel's built-in authentication routing using php artisan make:auth. When you run that command, these new routes are added to your web.php file:

我最近在使用 Laravel 的内置身份验证路由后遇到了这个错误php artisan make:auth。当您运行该命令时,这些新路由将添加到您的 web.php 文件中:

Auth::routes();

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

I must have accidentally deleted these routes. Running php artisan make:authagain restored the routes and solved the problem. I'm running Laravel 5.5.28.

我一定是不小心删除了这些路由。php artisan make:auth再次运行恢复路由,问题解决。我正在运行 Laravel 5.5.28。

回答by Cengkuru Michael

Am late to the party. if your expectation is some sort of json returned other than being redirected, then edit the exception handler so do just that.

我参加聚会迟到了。如果您的期望是某种 json 返回而不是被重定向,那么编辑异常处理程序就这样做。

Go to go to App\Exceptions\Handler.phpThen edit this code:

转到App\Exceptions\Handler.php然后编辑此代码:

public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }

to

public function render($request, Exception $exception)
    {
        return response()->json(
            [
                'errors' => [
                    'status' => 401,
                    'message' => 'Unauthenticated',
                ]
            ], 401
        );
    }

回答by Faiyaz Haider

If someone getting this from a rest client (ex. Postman) - You need to set the Header to Accept application/json.

如果有人从休息客户端(例如邮递员)获取此信息 - 您需要将标题设置为接受应用程序/json。

To do this on postman, click on the Headers tab, and add a new key 'Accept' and type the value 'application/json'.

要在邮递员上执行此操作,请单击“标题”选项卡,然后添加一个新键“接受”并键入值“应用程序/json”。