我如何获得资源存储路线laravel

时间:2018-12-01 12:50:21

标签: laravel laravel-routing laravel-5.7

我的添加表单代码

<form method="post" name="m_form" action="{{url('post')}}">
    <input type="hidden" name="_method" value="POST">
    <input type="hidden" name="_token" value="zL9O2D3H70fkSrt9vziwbkf2bHBqi6O6NKk1QgQK">
    <input type="text" required="" name="name" id="name" class="form-control m-input" >
    <input type="submit" name="submit" value="submit">
<form>

我的路线

Route::resource('post', 'PostController');

中间件

public function handle($request, Closure $next)
{
    $nextRequest = $next($request);
    dd($request->route()->getAction());
}

现在中间件 $ request-> route()-> getAction()为空

现在我要动态地在这里使用控制器名称和方法名称,所有其他路由都可以正常工作,但是找不到存储路由

您能指导我在哪里出错?

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为这就是您要寻找的.....

How to get name of requested controller and action in middleware Laravel

而且仅供参考,您不应该像示例中那样使用硬代码来设置_token,而应该使用Form :: open(),因为每次加载页面时都会为您设置_token。与您的硬编码版本相比,这显然是一种更加安全的方式,可以确保_token不会被强制执行,显然,硬编码版本每次都会为所有用户将_token设置为相同的值。

答案 1 :(得分:0)

我使用Laravel Log Class在log(/storage/logs/filename.log)文件中记录值,以观察输出。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;

class CheckControllerAction
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $action = Route::current()->action;
        $route = $action['uses'];
        $controllerAction = explode("@", $route);

        Log::info(class_basename($controllerAction[0]));
        Log::info($controllerAction[1]);
        Log::info(class_basename(Route::current()->controller));
        return $next($request);
    }
}

日志文件中的输出为:

[2018-12-04 21:34:41] local.INFO: PostController  
[2018-12-04 21:34:41] local.INFO: create  
[2018-12-04 21:34:41] local.INFO: PostController