Passing two values to the nested restful controller

ε祈祈猫儿з 提交于 2019-12-11 06:09:18

问题


In my nested restful controller my show method should receives two values like this

public function show($postId, $commentsId)
{

// code

}

the generated url should be like this

http://localhost/posts/1/comments/1

Now my query is: I need to send those twu value through my route call

I am using like this :

<a href="{{ URL::route('posts.comments.show', value1, value2) }}"> <h3> Click </h3></a>

But it is giving an error like this

Symfony \ Component \ Routing \ Exception \ InvalidParameterException
Parameter "dcoms" for route "debates.dcoms.show" must match "[^/]++" ("" given) to generate a corresponding URL.

回答1:


You can do it by declaring additional route.

You should have something like this:

Route::controller('posts', 'RestController');

Try to add below following line:

Route::controller('posts/{postId}/comments/{commentsId}', 'RestController@anotherShow');

And inside your function you have to add function for this route:

public function anotherShow($postId, $commentsId)
{

    // code

}

You have also change your URL::route like this:

URL::route('posts.comments.show', array(value1, value2))

Or:

URL::route('posts.comments.show', array('postId'=>value1, 'commentsId'=>value2))



回答2:


Now this problem is solved. I am adding this answer here cause It might help you to solve this type of problem. I want to thank @FDL for his awesome support and also to @kasys .

I had done a single mistake there as I have guessed in comment to the @kasys (@FDL solved this) in generating the url.

In this case the url generating method should be like this

<a href="{{ URL::route('posts.comments.show', array(value1, value 2)) }}"><h3>Click this link</h3></a>

here the value1 means the id of posts and value2 means the id of comments so url would be generated by this link is as ->

http://localhost/posts/value1/comments/value2

Thank you @FDL and all those fiends who have participated here. Soon a website will come to rock the world. need your blessings friends :) Thanks !



来源:https://stackoverflow.com/questions/19810034/passing-two-values-to-the-nested-restful-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!