【问题标题】:How can I get Actual Route path From Route file in controller using Laravel 5.4如何使用 Laravel 5.4 从控制器中的路由文件中获取实际路由路径
【发布时间】:2018-03-27 19:53:42
【问题描述】:

如何使用 Laravel 5.4 从控制器中的路由文件中获取实际路由路径

当我使用 $url =$request->route()->getPath(); 时,在 laravel 5.4 中出现错误消息

这样的消息:

  • local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: 在 E:\xampp\htdocs\newneptune\app\Http\Middleware\rollwise.php 中调用未定义的方法 Illuminate\Routing\Route::getPath() :25 堆栈跟踪:

【问题讨论】:

  • $url = Route::getCurrentRoute()->getPath(); 工作吗?
  • 不,它不工作。

标签: php laravel routes laravel-5.4


【解决方案1】:

使用getPathInfo():

$request->getPathInfo();

如果有的话,它会返回不带 GET 参数的 URI。

【讨论】:

  • 它看起来像 "/admin/clients/edit/35" 但我希望它像这样 "admin/clients/edit/{id}" 基于路由文件中的定义..跨度>
  • @punit:您可能希望将该信息添加到您的问题中,以便其他用户可以快速看到它
  • @punit 您需要的数据是闭包。你可以用$request->getRouteResolver() 得到它(见use > route > uri),但我不确定是否可以从闭包中得到它。
  • @punit request()->getRouteResolver() 在 5.2 中为我工作,但它返回一个包含您正在寻找的数据的闭包。
【解决方案2】:

使用Route::getCurrentRoute()->getUri()

希望这会有所帮助。

【讨论】:

  • local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Illuminate\Routing\Route::getUri() in E:\xampp\htdocs\newneptune\app\Http\Middleware\rollwise.php:26 Stack trace: 发生错误
  • 你用过Route::getUri() ?
【解决方案3】:

$request->route()->uri()代替$request->route()->getPath()

【讨论】:

  • 已解决,谢谢@krishna
猜你喜欢
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2017-11-21
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多