简体   繁体   中英

Laravel : send data to a controller method with a POST request

I set a nested resource like this in my routes.php file :

Route::resource('channels','ChannelsController');
Route::resource('channels.posts','PostsController');

and so when i want to show all posts on a given channel I would get the channel id form the URI : GET /channels/{channelId}/posts with the method :

// PostsController.php
/**
 * Display a listing of the resource.
 * GET channels/{channelId}/posts/
 * @return Response
 */
public function index($channelId)
{ 
      ...
}

but when i want to POST, the channel id will not get passed to the store method

// PostsController.php
/**
 * Store a newly created post whithin a channel
 * POST channels/{channelId}/posts/
 * @return Response
 */
public function store($channelId)
{
      ... // $channelId is not set
}

I know there's a solution, passing the data with a hidden field in the form, but it is not secure since anyone can edit it and post the wrong id. Please let me know, if you have any solution.

Actually, I found the solution in Laravel documentation itself, here is it so that everyone can take advantage from :

Form::open(array('action' => array('Controller@method', $user->id)))

  • the variable $user->id is passed as argument to the method method , also this last one should recieve an argument as well, like so : method($userId)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM