简体   繁体   English

Laravel:路由[登录]未定义

[英]Laravel: Route [login] not defined

I am creating an application in Laravela and when I try to visit my url, for example: http://example.com it is returning this weird error?我正在 Laravela 中创建一个应用程序,当我尝试访问我的 url 时,例如: http : //example.com它返回这个奇怪的错误? This only happens when I am not visiting /login?这仅在我不访问 /login 时发生?

InvalidArgumentException in UrlGenerator.php line 304:
Route [login] not defined.

Code:代码:

use App\Http\Controllers\Frontend;

Route::group(['domain' => 'localhost', 'namespace' => 'Frontend'], function() {

    Route::group(['middleware' => 'guest', 'namespace' => 'Guest'], function() {
        Route::get('/', function() { return Redirect::to('/login'); });
        Route::get('/login', 'LoginController@getView');
        Route::post('/login', 'LoginController@onPost');
    });

    Route::group(['middleware' => 'auth', 'namespace' => 'User'], function() {
        Route::get('/', function() { return Redirect::to('/home'); });
        Route::get('/home', 'HomeController@getView');
        Route::get('/logout', 'LogoutController@performLogout');
    });
});

Route::group(['domain' => 'admin.localhost'], function() {
    Route::get('/', function() {
        return 'Housekeeping will be making an appearance soon!';
    });
});

Here is my LoginController:这是我的 LoginController:

<?php
namespace App\Http\Controllers\Frontend\Guest;

use Auth;
use App\User;
use App\Http\Controllers\Controller;
use Validator;
use Redirect;
use Illuminate\Http\Request;
use App\Database\Frontend\User\Player;
use App\Database\Frontend\WebsiteLogin;

class LoginController extends Controller
{
    public function getView()
    {
        return view('frontend.login');
    }

    public function onPost(Request $request) 
    {
        $validator = Validator::make($request->all(), [
            'mail'     => 'required|email|exists:users',
            'password'  => 'required'
        ]);

        if ( $validator->fails()) {
            return Redirect::to('/login')->withErrors($validator->messages());
        }
        else {
            if (!Auth::attempt(['mail' => $request->input('mail'), 'password' => $request->input('password')])) {
                $this->addNewWebsiteLogin($request, Player::where('mail', $request->input('mail'))->pluck('id')->first(), "0");
                return Redirect::to('/login')->withMessage('Email and password do not match')->withColor('danger');
            }
            else {
                $this->addNewWebsiteLogin($request, Auth::user()->id, "1");

                $user = Auth::user();
                $user->last_online = time();
                $user->save();

                /*if (config('frontend.government_only') && (Auth::Guest() || Auth::user()->roleplay->government_id == 0)) {
                    Auth::logout();
                    return Redirect::to('/login')->withMessage(config('frontend.site_name') . ' is only open to government individuals at this moment, too bad.')->withColor('danger');
                }*/

                return Redirect::to('/home')->withMessage('Welcome back!');
            }
        }
    }

    private function addNewWebsiteLogin(Request $request, $userId, $status) {
        $websiteLogin                    = new WebsiteLogin;
        $websiteLogin->user_id           = $userId;
        $websiteLogin->request_ip        = $request->ip();
        $websiteLogin->request_system    = 'TODO';
        $websiteLogin->request_browser   = 'TODO';
        $websiteLogin->login_status      = $status;
        $websiteLogin->save();
    }
}

Looks like you're using route('login') to create a link to the login page or in form.看起来您正在使用route('login')来创建指向登录页面或表单的链接。

If you're using it in a link, just name get() route:如果您在链接中使用它,只需命名get()路由:

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

If you're using the route() helper in a form, do this for the post() route.如果您在表单中使用route()助手,请对post()路由执行此操作。

You can use the artisan comp php artisan make:auth to scaffold the authentication pages needed.您可以使用 artisan comp php artisan make:auth来搭建所需的身份验证页面。

You can find the documentation for that here, https://laravel.com/docs/5.4/authentication您可以在此处找到相关文档, https://laravel.com/docs/5.4/authentication

if you have, login route like this:如果你有,登录路线是这样的:

Route::post('login', ['middleware' => 'guest', 'uses' => 'UserController@postLogin',]);

just need to add :只需要添加

->name('login');

at the end of your route s.th like this.在你的路线结束时像这样。

your result s.th like this:你的结果是这样的:

Route::post('login', ['middleware' => 'guest', 'uses' => 'UserController@postLogin',])->name('login');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM