Laravel:路由 [登录] 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43052410/
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
Laravel: Route [login] not defined
提问by
I am creating an application in Laravela and when I try to visit my url, for example: http://example.comit is returning this weird error? This only happens when I am not visiting /login?
我正在 Laravela 中创建一个应用程序,当我尝试访问我的 url 时,例如:http: //example.com它返回这个奇怪的错误?这仅在我不访问 /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();
}
}
采纳答案by Alexey Mezenin
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()
路由执行此操作。
回答by William
You can use the artisan comp php artisan make:auth
to scaffold the authentication pages needed.
您可以使用 artisan compphp 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