在laravel 5中如何防止用户直接访问路径以及如何检查请求是空的?
Here are my routes in my laravel 5 application,
Route::get('demotest', 'HomeController@demo_test');
Route::post('demo_result', 'HomeController@demo_result');
In my first route view i have a from with action {{ url('demo_result') }}
if a user submits the form it will post to my second route and everything working fine, here after submitting form my route changing to demo_result
which is an post route.
Now the problem is if any user directly trying to access demo_result
it is showing following error.
So how to handle the issue. I tried to check whether the request is empty but no use. I am using
use Illuminate\Http\Request;
to handle request.
以下是我的laravel 5应用程序中的路线,
p>
Route :: get('demotest','HomeController @ demo_test');
code> pre>
Route :: post('demo_result','HomeController @demo_result'); code>
在我的第一个路线视图 strong>我有一个来自动作 {{url('demo_result')} } code>如果用户提交表格,它将发布到我的第二条路线并且一切正常,在提交表格之后我的路线改为 demo_result code>这是一条后路线。
Now 问题是如果任何用户直接尝试访问 demo_result code>,它会显示以下错误。
如何处理问题。 我试图检查请求是否为空但没有用。 我正在使用使用Illuminate \ Http \ Request; code>来处理请求。 p>
div>
Use any
instead of get
or post
and then by using isset
you can write your own login. Try this Route::any();
Method not allowed means you haven't declared a GET method for your demo_result URL.
What you can do I think is just creating a new route
Route::get(demo_result', ...);
That point to a custom method in your controller that redirect the user to your form, like
public function getResult(){ return redirect('demo_form');
When you are not posting data and just visiting the page from your browser you are asking a different method, not what you have declared.
Another way is just to set up a 404 handler...
You can edit your Render Function in
app\Exceptions\handler.php
like:
public function render($request, Exception $exception)
{
if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
return redirect('/');
}
return parent::render($request, $exception);
}