laravel 404 中止如何执行此操作

laravel 404 abort how to do this

我的问题是我不会写代码,工作视图 404... 我的网址是 f.e : http://hospital.com 我的路线是这样的:

 Route::post('/panel/perscriptions/add','PrescriptionsController@addPrescription');
 Route::post('/panel/perscriptions/delete','PrescriptionsController@deletePerscription');

在处理程序上我有这样的代码:

public function render($request, Exception $exception)
{
    if($exception instanceof NotFoundHttpException)
    {
        return response()->view('errors.404', [], 404);
    }
    return parent::render($request, $exception);
}

例如我的网址是:http://hospital.com 当然,当我用 url 字写的时候它是有效的,这不是我的路线: http://hospital.com/someWordWhichIsNotInRoutes

然后我的 404 视图可以工作,但是当我粘贴 url 时:http://hospital.com/panel/perscriptions/add 然后我有错误:

MethodNotAllowedHttpException

当然,用户必须登录才能添加权限,但是当我粘贴此内容时并不重要url...我总是出错。没有,当用户只是添加 perscipritpion 时,只有当我将其独立粘贴到 url 中时。 这是我的控制器功能:

public function deletePerscription(Request $request)
{

    $Id = $request->input('id');

    $deleteRow = Perscription::where('id', $Id)->delete();

    return redirect('/panel/visits')->with('info', 'deleted');;

}

我真的坚持这个...:(

我无法保护我的代码...

NotFoundHttpExceptionMethodNotAllowedHttpException 是不同类型的异常。在第一种情况下,当您尝试访问 URL http://hospital.com/someWordWhichIsNotInRoutes 时,它根本不存在并抛出 NotFoundHttpException,这将为您提供 404 页面。但是对于第二种情况,URL http://hospital.com/panel/perscriptions/add 存在但它只允许 POST 请求。当您尝试通过浏览器访问它时,它会发送 GET 请求而不是 POST 请求。由于您的路由不允许此路由的 GET 请求,因此它会抛出 MethodNotAllowedHttpException 异常。您可以通过在 Handler class.

中添加一个条件来解决这个问题
if($exception instanceof MethodNotAllowedHttpException)
{
    // do the redirect here
}

别忘了导入 class use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;