欢迎各位兄弟 发布技术文章
这里的技术是共享的
PUBLISHED 1 YEAR AGO BY NICKLAW5
Is it possible to pass a subdomain variable through to a controller via the routes.php file?
If possible, I'd like to use route resources, rather than defining every route individually?
For example:
domain 好像不能跟其它的在一组
比如 Route::group(['doamin'=>'{account}.wangzhan.com','namespace' => 'Front'],function($account){
就有问题 就有问题
// /app/Http/routes.php
Route::group(['domain' => '{company}.myapp.com'], function() {
Route::group(['prefix' => 'api/v1'], function($company) {
Route::resource('users', 'UsersController'); // pass $company here somehow???
});
});
Then in the controller when the index method is hit:
// /app/Http/Controllers/UsersController.php
...
public function index() {
return $company;
}
...
Is something this like that possible?
Hello,
you can find here http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing
public function index($company)
{
return $company;
}
Thanks @pmall, that was about the only thing I didn't try :)
来自 https://laracasts.com/discuss/channels/laravel/how-to-pass-subdomain-variables-to-controller
发表于1年前 BY NICKLAW5
可以通过routes.php文件将子域变量传递给控制器吗?
如果可能,我想使用路由资源,而不是单独定义每个路由?
例如:
// /app/Http/routes.php
Route::group(['domain' => '{company}.myapp.com'], function() {
Route::group(['prefix' => 'api/v1'], function($company) {
Route::resource('users', 'UsersController'); // pass $company here somehow???
});
});
然后在控制器中的index方法被命中:
// /app/Http/Controllers/UsersController.php
...
public function index() {
return $company;
}
...
这是可能的东西吗?
来自 https://laracasts.com/discuss/channels/laravel/how-to-pass-subdomain-variables-to-controller
Route groups may also be used to route wildcard sub-domains. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the domain
key on the group attribute array:
Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
The prefix
group array attribute may be used to prefix each route in the group with a given URI. For example, you may want to prefix all route URIs within the group with admin
:
Route::group(['prefix' => 'admin'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
You may also use the prefix
parameter to specify common parameters for your grouped routes:
Route::group(['prefix' => 'accounts/{account_id}'], function () {
Route::get('detail', function ($account_id) {
// Matches The accounts/{account_id}/detail URL
});
});
来自 https://laravel.com/docs/5.1/routing#route-group-sub-domain-routing
PUBLISHED 1 YEAR AGO BY RONNYANDRE
I have a method called domain()
for checking subdomain in a model Contest
. In my QuestionController
I currently do this to fetch the correct contest, based on the subdomain:
$questions = Contest::with('questions.alternatives')->domain($account)->first();
The $account
variable comes from the route:
Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
Route::resource( 'questions', 'QuestionController' );
}
But in the QuestionController
constructor the $account
variable is not available. How can I access it?
You need to pass $account into the function, so I don't think you can do it with REST resource controllers.
if your function was askQuestion
Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
Route::get('/question/ask', 'QuestionController@askQuestion($account)' );
}
but if all your routes need to capture the domain then you should store this as application context so that it is available across the application
Most part of my application require the subdomain, I will check that out. Thanks for the tips! :)
由RONNYANDRE 发表于1年前
我有一个方法,domain()
用于在模型中检查子域Contest
。在我目前QuestionController
我正在做的这个取决于正确的比赛,基于子域名:
$questions = Contest::with('questions.alternatives')->domain($account)->first();
该$account
变量来自于路线:
Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
Route::resource( 'questions', 'QuestionController' );
}
但是在QuestionController
构造函数中,$account
变量不可用。如何访问?
您需要将$帐户传递到该函数中,所以我不认为您可以使用REST资源控制器。
如果你的功能是 askQuestion
Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
Route::get('/question/ask', 'QuestionController@askQuestion($account)' );
}
但是如果所有路由都需要捕获域,那么您应该将其存储为应用程序上下文,以便在整个应用程序中可用
我的大部分应用程序需要子域名,我会检查一下。感谢您的提示!:)
来自 https://laracasts.com/discuss/channels/laravel/how-to-get-subdomain-from-route-variable-in-controller