Laravel 中 RouteCollection.php 中的 NotFoundHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43913408/
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
NotFoundHttpException in RouteCollection.php in Laravel
提问by prakash pokhrel
Route file
路由文件
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/' , ['as' => '/', 'uses' => 'LoginController@getlogin']);
Route::post('/Login', ['as'=> 'Login' , 'uses' => 'LoginController@postLogin' ]);
Route::get('/login', array('as' => 'login', 'uses' => 'LoginController@getLogin'));
Route::group(['middleware'=>['authen','roles' ]], function(){
Route::get('/logout' , ['as' => 'logout' , 'uses'=> 'LoginController@getLogout']);
Route::get('/dashboard',['as'=> 'dashboard', 'uses'=> 'DashboardController@dashboard']);
});
loginController
登录控制器
class LoginController extends Controller{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/dashboard';
protected $guard = 'web';
public function getLogin()
{`enter code here`
if (Auth::guard('web')->check()){
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt(['username'=>$request->username, 'password'=>$request->passwod , 'active' => 1]);
if ($auth) {
return redirect()->route('dashboard');
}
return redirect()-> route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
Whenever I try to login then the url goes to "http://localhost:8000/login " and NotFoundHttpException in RouteCollection.php line 179: error happen.
每当我尝试登录时,网址都会转到“ http://localhost:8000/login ;” 和 RouteCollection.php 第 179 行中的 NotFoundHttpException:发生错误。
I tried lots of time but I can't log into Laravel.
我尝试了很多时间,但我无法登录 Laravel。
The Blade file
刀片文件
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }} ">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="email" type="username" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password"
required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
采纳答案by Parth Vora
Remove the last spacefrom the action attribute of the form:
从表单的 action 属性中删除最后一个空格:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }} ">
should be:
应该:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
Also, make letter Lsmall in login post route.
另外,在登录后路由中将字母L 变小。
Route::post('/Login', ['as'=> 'Login' , 'uses' => 'LoginController@postLogin' ]);
Should be
应该
Route::post('/login', ['as'=> 'Login' , 'uses' => 'LoginController@postLogin' ]);
回答by Said El-Ghamri
try http://localhost:8000/yourProjetcName/logininstead of localhost:8000/login
 if it does not work Try to access http://localhost:8000/yourProjetcName/index.php/loginIf it works, then you need to set up a virtual Host in your apache vHosts file
尝试http://localhost:8000/yourProjetcName/login而不是 localhost:8000/login 如果它不起作用尝试访问http://localhost:8000/yourProjetcName/index.php/login如果它起作用,那么你需要在你的 apache vHosts 文件中设置一个虚拟主机
回答by I Want Answers
I received this error when attempting to access an API route proxied by a middleware redirecting to another url when a certain condition was unmet. I don't know how redirect requests are handled in respect to APIs in Laravel, but I kept getting the error in the OP until I inspected the request object at the end of the stack trace.
我在尝试访问由中间件代理的 API 路由时收到此错误,该路由在未满足特定条件时重定向到另一个 url。我不知道 Laravel 中如何处理与 API 相关的重定向请求,但我一直在 OP 中收到错误消息,直到我检查堆栈跟踪末尾的请求对象。
I've googled for the specific behavior during API redirects but the only questions that keep popping up is something along the lines of "redirect to login". Nobody seems to have ever experienced what happens when an endpoint returns the response from a redirect.
我已经在 API 重定向期间搜索了特定行为,但唯一不断出现的问题是“重定向到登录”之类的问题。似乎没有人经历过当端点从重定向返回响应时会发生什么。
So, since fixing the redirect in the middleware solved it for me, I'll assume this is the default behavior, and advise future visitors to this question to check the middlewares attached to their intended route.
因此,由于修复中间件中的重定向为我解决了这个问题,我将假设这是默认行为,并建议此问题的未来访问者检查附加到他们预期路由的中间件。
In retrospect, throwing an error seems like a logical response (nevermind the NotFoundHttpException error not exactly being helpful in diagnosing what went wrong). The page you are being redirected to may require action such as requesting input, which client is in no position to fill. In such case, the response really is of no use.
回想起来,抛出错误似乎是一个合乎逻辑的响应(别说 NotFoundHttpException 错误对诊断出了什么问题没有帮助)。您被重定向到的页面可能需要诸如请求输入之类的操作,而客户端无法填充该页面。在这种情况下,响应确实没有用。
回答by CloudGuru
ssh
to your server via putty (Windows) or terminal (Mac) and change ownership of the directory like so:
ssh
通过 putty (Windows) 或终端 (Mac) 连接到您的服务器,并像这样更改目录的所有权:
chown -R daemon:daemon /your/script/directory
chown -R daemon:daemon /your/script/directory