如何将路由值传递给 Laravel 4 中的控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18166751/
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
How to pass route values to controllers in Laravel 4?
提问by uberHasu
I am struggling to understand something that I am sure one of you will be able to easily explain. I am somewhat new to MVC so please bear with me.
我正在努力理解一些我相信你们中的一个人能够轻松解释的事情。我对 MVC 有点陌生,所以请多多包涵。
I have created a controller that handles all of the work involved with connecting to the Twitter API and processing the returned JSON into HTML.
我创建了一个控制器来处理与连接到 Twitter API 和将返回的 JSON 处理成 HTML 相关的所有工作。
Route::get('/about', 'TwitterController@getTweets');
I then use:
然后我使用:
return View::make('templates.about', array('twitter_html' => $twitter_html ))
Within my controller to pass the generated HTML to my view and everything works well.
在我的控制器中将生成的 HTML 传递给我的视图,一切正常。
My issue is that I have multiple pages that I use to display a different Twitter user's tweets on each page. What I would like to do is pass my controller an array of values (twitter handles) which it would then use in the API call. What I do not want to have to do is have a different Controller for each user group. If I set $twitter_user_ids
within my Controller I can use that array to pull the tweets, but I want to set the array and pass it into the Controller somehow. I would think there would be something like
我的问题是我有多个页面用于在每个页面上显示不同的 Twitter 用户的推文。我想要做的是向我的控制器传递一个值数组(twitter 句柄),然后它将在 API 调用中使用。我不想做的是为每个用户组使用不同的控制器。如果我$twitter_user_ids
在我的控制器中设置,我可以使用该数组来提取推文,但我想设置该数组并将其以某种方式传递到控制器中。我认为会有类似的东西
Route::get('/about', 'TwitterController@getTweets('twitter_id')');
But that last doesn't work.
但最后那个不起作用。
I believe that my issue is related to variable scope somehow, but I could be way off.
我相信我的问题以某种方式与变量范围有关,但我可能离题了。
Am I going down the wrong track here? How do I pass my Controllers different sets of data to produce different results?
我在这里走错了路吗?我如何通过我的控制器不同的数据集来产生不同的结果?
EDIT - More Info
编辑 - 更多信息
Markus suggested using Route Parameters, but I'm not sure that will work with what I am going for. Here is my specific use case.
Markus 建议使用路由参数,但我不确定这是否适用于我的目的。这是我的具体用例。
I have an about page that will pull my tweets from Twitters API and display them on the page. I also have a "Tweets" page that will pull the most recent tweets from several developers accounts and display them.
我有一个关于页面,可以从 Twitter API 中提取我的推文并将它们显示在页面上。我还有一个“推文”页面,可以从几个开发人员帐户中提取最新的推文并显示它们。
In both cases I have $twitter_user_ids = array() with different values in the array.
在这两种情况下,我都有 $twitter_user_ids = array() 在数组中具有不同的值。
The controller that I have built takes that array of usernames and accesses the API and generates HTML which is passed to my view.
我构建的控制器采用该用户名数组并访问 API 并生成传递给我的视图的 HTML。
Because I am working with an array (the second of which is a large array), I don't think that Route Parameters will work.
因为我正在使用一个数组(其中第二个是一个大数组),所以我认为 Route Parameters 不会起作用。
Thanks again for the help. I couldn't do it without you all!
再次感谢您的帮助。没有你们我做不到!
回答by Markus Hofmann
First of all, here's a quick tip:
首先,这里有一个快速提示:
Instead of
代替
return View::make('templates.about', array('twitter_html' => $twitter_html ))
...use
...用
return View::make('templates.about', compact('twitter_html'))
This creates the $twitter_html automatically for you. Check it out in the PHP Manual.
这会自动为您创建 $twitter_html。在PHP 手册中查看。
Now to your problem:
现在你的问题:
You did the route part wrong. Try:
你做错了路线部分。尝试:
Route::get('/about/{twitter_id}', 'TwitterController@getTweets');
This passes the twitter_id param to your getTweets function.
这会将 twitter_id 参数传递给您的 getTweets 函数。
Check out the Laravel Docs: http://laravel.com/docs/routing#route-parameters
查看 Laravel 文档:http://laravel.com/docs/routing#route-parameters