欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to pass subdomain variables to controller 在控制器中访问子域名变量 有大用

How to pass subdomain variables to controller

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?

Best Answer(As Selected By nicklaw5)
pmall
public function index($company)
{
    return $company;
}
pmall
 pmall
1 year ago(575,495 XP)
public function index($company)
{
    return $company;
}
 

如何将子域变量传递给控制器

发表于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;
}
...

这是可能的东西吗?

最佳答案(由nicklaw5选定)
pmall
public function index($company)
{
    return $company;
}
pmall
 PMALL
1年前(575,495 XP)
public function index($company)
{
    return $company;
}
 
nicklaw5
 nicklaw5
1年前(3,125 XP)

来自  https://laracasts.com/discuss/channels/laravel/how-to-pass-subdomain-variables-to-controller
 

Sub-Domain Routing

 

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) {
        //
    });
});

Route Prefixes

 

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

How to get subdomain from Route variable in controller

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?

Best Answer(As Selected By ronnyandre)
Snapey

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

Snapey
 Snapey
1 year ago(489,705 XP)

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

 
ronnyandre

Most part of my application require the subdomain, I will check that out. Thanks for the tips! :)



来自 https://laracasts.com/discuss/channels/laravel/how-to-get-subdomain-from-route-variable-in-controller



如何从控制器中的Route变量获取子域

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变量不可用。如何访问?

最佳答案(由ronnyandre选定)
Snapey

您需要将$帐户传递到该函数中,所以我不认为您可以使用REST资源控制器。

如果你的功能是 askQuestion

Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
    Route::get('/question/ask', 'QuestionController@askQuestion($account)' );
}

但是如果所有路由都需要捕获域,那么您应该将其存储为应用程序上下文,以便在整个应用程序中可用

Snapey
 Snapey
1年前(489,705 XP)

您需要将$帐户传递到该函数中,所以我不认为您可以使用REST资源控制器。

如果你的功能是 askQuestion

Route::group( [ 'domain' => '{account}.advent.dev' ], function () {
    Route::get('/question/ask', 'QuestionController@askQuestion($account)' );
}

但是如果所有路由都需要捕获域,那么您应该将其存储为应用程序上下文,以便在整个应用程序中可用

 
ronnyandre
ronnyandre
1年前(9,025 XP)

我的大部分应用程序需要子域名,我会检查一下。感谢您的提示!:)

来自 https://laracasts.com/discuss/channels/laravel/how-to-get-subdomain-from-route-variable-in-controller


普通分类: