php 将参数从laravel中的路由传递给控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12647424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:57:46  来源:igfitidea点击:

Passing parameter to controller from route in laravel

phplaravel

提问by bretterer

THIS IS A QUESTION FOR LARAVEL 3

这是 Laravel 3 的问题

Given the following route

鉴于以下路线

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show'));

And the URL I would like to use,

还有我想使用的网址,

http://www.example.com/groups/1

I would like to be able to use the (:any)value in my controller.

我希望能够(:any)在我的控制器中使用该值。

My controller looks like

我的控制器看起来像

class Groups_Controller extends Base_Controller {

    public $restful = true;    

    public function get_show($groupID) {
        return 'I am group id ' . $groupID;
    }  


}

How is this possible to do? I have tried a few things including the following

这怎么可能呢?我尝试了一些事情,包括以下内容

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show((:1))'));

but it did not work.

但它没有用。

UPDATE

更新

Anytime I try to pass in the arguments as show above i get a 404 error.

每当我尝试按上面显示的方式传递参数时,我都会收到 404 错误。

Thanks for the help!

谢谢您的帮助!

采纳答案by dardub

You don't need anything special for adding paramaters. Just like you had it.

您不需要任何特殊的东西来添加参数。就像你拥有它一样。

Route::get('groups/(:any)', array('as' => 'group', 'uses' => 'groups@show'));


class Groups_Controller extends Base_Controller {

    public $restful = true;    

    public function get_show($groupID) {
        return 'I am group id ' . $groupID;
    }  


}

回答by Eddy Ferreira

This is what you need in 1 line of code.

这是您在 1 行代码中所需要的。

Route::get('/groups/{groupId}', 'GroupsController@getShow');

Route::get('/groups/{groupId}', 'GroupsController@getShow');

Suggestion: Use CamelCase as opposed to underscores, try & follow PSR-* guidelines.

建议:使用 CamelCase 而不是下划线,尝试遵循PSR-* 指南

Hope it helps.

希望能帮助到你。

回答by shaggy

You can add them like this

你可以像这样添加它们

  Route::get('company/{name}', 'PublicareaController@companydetails');

回答by Hashmat Waziri

    $ php artisan route:list
  +--------+--------------------------------+----------------------------+--    -----------------+----------------------------------------------------+---------  ---+
  | Domain | Method                         | URI                        |  Name              | Action                                             |    Middleware |
  +--------+--------------------------------+----------------------------+-------------------+----------------------------------------------------+------------+
  |        | GET|HEAD                       | /                          |                           
  |        | GET                            | campaign/showtakeup/{id}   | showtakeup         | App\Http\Controllers\campaignController@showtakeup | auth       |     |

routes.php

路由文件

  Route::get('campaign/showtakeup/{id}', ['uses' =>'campaignController@showtakeup'])->name('showtakeup');

campaign.showtakeup.blade.php

活动.showtakeup.blade.php

 @foreach($campaign as $campaigns)


   //route parameters; you may pass them as the second argument to the method:

   <a href="{{route('showtakeup', ['id' => $campaigns->id])}}">{{ $campaigns->name }}</a>




            @endforeach

Hope this solves your problem. Thanks

希望这能解决您的问题。谢谢