dpbfb7119 2015-09-30 06:33
浏览 135
已采纳

Laravel 5.1从漂亮的URL获取变量到Request对象

I have a controller method in Laravel 5.1 that takes both GET and POST requests.

There are some variables I have to pass in to that method.

So I'd pass the variable in like this:

http://localhost/<methodName>/<var1 value>/<var2 value>

I'd like Laravel's Request object to be populated with those variables.

Of course I can do:

http://localhost/<MethodName>?var1=var1_value&var2=var2_value

But I'd like to keep the pretty url mentioned before and still be able to populate Request object.

public function methodName(Request $request)
{
    dd($request);
}

This return an empty array.

Yes, I can do something like:

public function methodName(Request $request, $var1= "", $var2 = "")
{
    dd($var1." ".$var2);
}

This will return the variables but I'd really like to have the Request object populated if possible.

The route I'm using is:

 Route::match(['get', 'post'],
 '/<method-name>/{<var1_value>?}/{<var2_value>?}/{<var3_value>?}',
 '<ControllerName>@<methodName>');

Post variables are not a problem.

  • 写回答

1条回答 默认 最新

  • doukao5073 2015-09-30 07:09
    关注

    In your controller, use route() function on $request object.

    $request->route('var1');
    

    Of course the name of parameter must match what you define in your routes.php

    Route::get('your/route/{var1}/{var2}','YourController@method');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用