将变量从路由组传递到 Laravel 4 中的路由函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20661182/
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
Passing variables from route group to route functions in Laravel 4
提问by Inigo
I have one installation of Laravel on which I wish to run 3 sites (addon domains). I am using Laravel's route grouping method to grab each domain. However, I need to be able to know which domain I am working with inside of each group. What is the best way to do this? I have the following code:
我有一个 Laravel 安装,我希望在其上运行 3 个站点(附加域)。我正在使用 Laravel 的路由分组方法来获取每个域。但是,我需要能够知道我在每个组内使用哪个域。做这个的最好方式是什么?我有以下代码:
Route::group(array('domain' => 'domainone.com'), function($domain = 'domainone')
{
Route::get('/', function($domain) {
//
});
});
^ Which doesn't work.
^ 这不起作用。
The notessuggest using wildcards in the URL, eg.
该票据建议在网址中使用通配符,例如。
Route::group(array('domain' => '{domain}.com'), function()
{
Route::get('/', function($domain) {
//
});
});
However, I would prefer a different method, as I can't really use this during development on my local server. Is there any way that is more like my first method, in which I can just manually declare a key for each domain?
但是,我更喜欢不同的方法,因为在本地服务器上的开发过程中我无法真正使用它。有什么方法更像我的第一种方法,我可以在其中手动为每个域声明一个键?
EDIT: I also then need to pass that domain variable to a controller, which I am also struggling to work out how to do?
编辑:然后我还需要将该域变量传递给控制器,我也在努力解决如何做?
Thanks.
谢谢。
EDIT 2
编辑 2
My problem is that I am not using subdomains, I am using domains; I have 3 separate domains for 3 sister sites that are running on the same installation of Laravel. So I have 3 route groups, one for each domain. Moreover, I don't want to requestthe domain variable using {domain}.com
each time, I want to tellLaravel the domain in each case, then pass that domain as a variable to the appropriate controller within each group. Here is my example code:
我的问题是我没有使用子域,我使用的是域;我有 3 个单独的域,用于运行在同一 Laravel 安装上的 3 个姐妹站点。所以我有 3 个路由组,每个域一个。此外,我不想每次都请求域变量{domain}.com
,我想在每种情况下告诉Laravel 域,然后将该域作为变量传递给每个组内的适当控制器。这是我的示例代码:
$domain1 = 'domain1.com';
$domain2 = 'domain2.com';
$domain3 = 'domain3.com';
Route::group(array('domain' => $domain1), function(){
Route::get('/', 'PrimaryController@somefunction1'); // <-- I want to access $domain1 in my controller
});
Route::group(array('domain' => $domain2), function(){
Route::get('/', 'PrimaryController@somefunction2'); // <-- ...and $domain2
});
Route::group(array('domain' => $domain3), function(){
Route::get('/', 'PrimaryController@somefunction3'); // <-- ...and $domain3
});
回答by Antonio Carlos Ribeiro
This is an option for your first method:
这是您的第一种方法的一个选项:
$domain = 'domainone';
Route::group(array('domain' => $domain.'.com'), function() use ($domain)
{
Route::get('/', function() use ($domain) {
echo "$domain";
});
});
You can pass watever you like to your controllers, via groups too, you just need to add one more level.
您可以通过组将任何您喜欢的水传递给您的控制器,您只需要再添加一个级别。
$subdomain = 'atlanta';
$domain = 'domainone';
Route::group(array('domain' => "$subdomain.$domain.com"), function()
{
Route::group(array('domain' => '{subdomain}.{domain}.com'), function()
{
Route::get('testdomain', function($subdomain, $domain) {
dd("closure: subdomain: $subdomain - domain: $domain");
});
Route::get('testdomaincontroller', 'FooController@index');
});
});
By doing this you have to understand that your first two variables passed to your controller action will always be $subdomain and $subdomain. Here's a controller to show it, which you can use to test those routes too:
通过这样做,您必须了解传递给控制器操作的前两个变量将始终是 $subdomain 和 $subdomain。这是一个显示它的控制器,您也可以使用它来测试这些路由:
class FooController extends Controller {
public function index($subdomain, $domain)
{
dd("controller: subdomain: $subdomain - domain: $domain");
}
}
You'll have two different routes with this:
您将有两条不同的路线:
http://yourdomain.dev/testdomain
http://yourdomain.dev/testdomaincontroller
回答by James Binford
I accomplish this with the following two steps:
我通过以下两个步骤完成此操作:
First, using the Laravel documentation example, I pull the subdomain down and assign it to {account}.
首先,使用 Laravel 文档示例,我拉下子域并将其分配给 {account}。
Route::group(array('domain' => '{account}.myapp.com'), function()
From here, any controller you assign to a route within this groupwill have the ability to inject this {account}data in to its functions.
从这里开始,您分配给该组内路由的任何控制器都可以将此{account}数据注入其功能。
Note: you can'taccess it in the constructor. Each function in the Controller that needs to use this data will need a parameter created for it. Like this:
注意:您不能在构造函数中访问它。Controller 中需要使用此数据的每个函数都需要为其创建一个参数。像这样:
/**
* The $subdomain variable below will capture the data stored in
* {account} in your route group. Note that you can name this variable
* anything you'd like.
*/
public function showCreateBankAccount($subdomain) {
echo "This is your subdomain: " . $subdomain;
}