草庐IT

关于php:Laravel post route 带url参数

codeneng 2023-03-28 原文

Laravel post route with url parameters

我正面临着一大堵幼虫路线,我似乎找不到解决方案

我在视图模板中有这个表单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<form url="/request/{{$equipment->url}}" method="POST">
                   
                       
                            Start Date:
                            <input type="date" required name="starting_date" value="" placeholder="From" class="request-input request-date mb10">
                       
                       
                            End Date:
                            <input type="date" required name="ending_date" value="" placeholder="To" class="request-input request-date mb10">
                       
                       
                            Quantity
                            <input type="number" required name="quantity" value="" placeholder="Quantity" class="request-input mb10">
                       
                   
                   
                       
                            Voltage
                            <input type="number" required name="voltage" value="" placeholder="Voltage" class="request-input mb10">
                       
                       
                            Param 1
                            <input type="text" required name="param_1" value="" placeholder="Parameter" class="request-input mb10">
                       
                       
                            Param 2
                            <input type="text" required name="param_2" value="" placeholder="Parameter" class="request-input mb10">
                       
                   
                   
                       
                            <button class="btn btn-block button-orange">Get quotes now</button>
                       
                   
                </form>

这是对应的路线

1
2
3
4
5
6
Route::group([ 'middleware' => 'rental'], function(){
 Route::get('/my-requests/{readby_url}', 'PagesController@requests');
 Route::post('/request/{equipment_url}', 'PagesController@request');
 Route::post('/request/create', 'RequestsController@create');
 Route::post('/request/accept', 'RequestsController@accept');
});

我的问题是 Route::post('/request/{equipment_url}', 'PagesController@request');
因为当方法设置为 post 时它似乎不接受 url 参数。

即它抛出错误

1
2
3
4
5
6
7
8
MethodNotAllowedHttpException in RouteCollection.php line 201:
in RouteCollection.php line 201
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 188
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 140
at RouteCollection->match(object(Request)) in Router.php line 746
at Router->findRoute(object(Request)) in Router.php line 655
at Router->dispatchToRoute(object(Request)) in Router.php line 631
at Router->dispatch(object(Request)) in Kernel.php line 237

我想同时传递一个参数和发布数据。

有没有办法让这个工作?有人告诉我 Route::post 也可以处理 GET,但它似乎不起作用。


问题与 Laravel 无关

1
<form url="/request/{{$equipment->url}}" method="POST">

url 替换为 action

1
<form action="/request/{{$equipment->url}}" method="POST">

  • Jad 在发布此问题的同一天回答 Jad 的几率是多少
  • 万亿比1。
  • 我阅读此评论并对其进行投票,然后在一年后回来并尝试再次对其进行投票,结果发现箭头是橙色的,因为我已经投票了


您不能将获取参数发送到发布路由。

但是您可以通过一个简单的技巧来实现,只需在表单的隐藏字段或会话中传递您的值 ({{$equipment->url}})。

例如:

html

1
2
3
4
5
6
<form url="test/{{$equipment->url}}" method="POST">
    {{Input::hidden('name-of-field', $equipment->url)
   
        .......
   
</form>

路线

1
Route::post('test/{any-variable}', ['as' => 'test', 'uses' => 'TestController@test']);

控制器

1
2
3
4
5
public function test()
{
    echo"[cc lang="php"]";
    dd(Input::all());
}

结果

1
2
3
4
array(1) {
           ["name-of-field"]=>
            string(5)"your value here"
          }

  • 我认为这个答案也没有抓住重点。您不能使用 POST 发送获取参数(QUERY STRING),但 OP 发布的路由使用 URI 段。 Laravel 很好地解决了它们,所以我看不到任何问题。


HTTP POST 动词不接受来自 URL 的参数,如 GET,它接受来自 HTTP POST 正文的参数。要获取发布参数,请使用以下代码:

在 routes.php:

1
Route::post('/request', 'PagesController@request');

并在您的 PagesController 中使用以下输入方法之一访问表单输入

1
2
3
4
public function request()
{
    return Input::get('equipment_url');
}

  • 问题是我希望 url 改变并显示设备 URL,这就是我使用路由参数的原因
  • @JadSalhani 那么你将不得不使用 GET
  • 这样做的动机是什么?在我的情况下,将参数作为 URL 的一部分传递会使事情变得更加方便:(
  • 我认为这个答案没有抓住重点。是的,POST 不接受 QUERY STRING 参数,但它接受 URI 段,这就是 OP 发布的路由所使用的。

有关关于php:Laravel post route 带url参数的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  2. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  3. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  4. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  5. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  6. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  7. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  8. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  9. ruby-on-rails - Ruby url 到 html 链接转换 - 2

    我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.

  10. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

随机推荐