laravel 如何在laravel 4中使用子域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19541527/
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-09-14 08:35:55  来源:igfitidea点击:

How to use subdomain in laravel 4?

laravellaravel-4

提问by Kapil Gupta

I want to detect subdomains in routes.php in my L4 website and want to store that subdomain value somewhere so that I can access that value in each controller.

我想在我的 L4 网站的 routes.php 中检测子域,并希望将该子域值存储在某处,以便我可以在每个控制器中访问该值。

How can I do that ? Please help

我怎样才能做到这一点 ?请帮忙

回答by ceejayoz

http://laravel.com/docs/routing#sub-domain-routing

http://laravel.com/docs/routing#sub-domain-routing

Route::group(array('domain' => '{account}.myapp.com'), function() {
    Route::get('user/{id}', function($account, $id) {
        //
    });
});

回答by kablamus

You can put this type of method in routes. However, I think it's a better fit for a filter in the 'app/filters.php' file. Try this:

您可以将这种类型的方法放在路由中。但是,我认为它更适合“app/filters.php”文件中的过滤器。尝试这个:

Route::filter('getSubdomain', function($route, $request) 
{
    $host = $request->getHost();
    $parts = explode('.', $host);
    $subdomain = $parts[0];

    // Store subdomain in session
    Session::put('subdomain', $subdomain);

});

Then add the filter to the route (probably a group route) as follows:

然后将过滤器添加到路由(可能是组路由)中,如下所示:

Route::group(array('before' => 'getSubdomain'), function()
{
 ... add route stuff here ..
});

You can read more about how to use Laravel filters here:

您可以在此处阅读有关如何使用 Laravel 过滤器的更多信息:

http://laravel.com/docs/routing#route-filters

http://laravel.com/docs/routing#route-filters

回答by Antonio Carlos Ribeiro

You can use Request to get your domain anywhere.

您可以使用 Request 在任何地方获取您的域。

So, create a BaseController and add a method to get the current domain on all your extended controllers:

因此,创建一个 BaseController 并添加一个方法来获取所有扩展控制器上的当前域:

class BaseController extends Controller {

    public function getDomain()
    {
        return Request::getHost();
    }

}

And use it:

并使用它:

class PostsController extends BaseController {

    public function store()
    {
        $post = new Post;

        $post->domain_id = Domain::where('name', $this->getDomain())->first()->id;

        $post->save();
    }

}

Of course, this controller example supposes that you have a Domain model:

当然,这个控制器示例假设您有一个域模型:

class Domain extends Eloquent {

    private $table = 'domains';

}

EDIT:

编辑:

Unless you have a very good reason for it, you don't have to use routes or store your subdomain on a session, unless you have a reallygood reason for this, it's a smell. Take a look at Laravel's code, there only one session stored by it: Laravel's session.

除非你有一个很好的理由,你不必使用路由或您的子存储在一个会话,除非你有一个真正好的理由,这是一个味道。看一下 Laravel 的代码,它只存储了一个会话:Laravel 的会话。

You can create a helper function:

您可以创建一个辅助函数:

Create a app/helpers/functions.php file (this is just an example) and add this helper function there:

创建一个 app/helpers/functions.php 文件(这只是一个例子)并在那里添加这个辅助函数:

function getCurrentSubdomain()
{
    $domain = Config::get('app.domain');

    preg_match("/^(.*)(\.$domain)$/", Request::getHost(), $parts);

    return $parts[1];
}

Open your app/config/app.php and add a domain configuration to it:

打开您的 app/config/app.php 并向其添加域配置:

return array(

    'domain' => 'myapp.com',

        ...
);

Add the file to the autoload section of your composer.json:

将文件添加到 composer.json 的自动加载部分:

"autoload": {
    "classmap": [
        ...
    ],
    "files": [
         "app/helpers/functions.php"
    ]

}, 

Then you can use it everywhere: controllers, classes, routers, etc. Here's the same example as before, using it:

然后你就可以在任何地方使用它:控制器、类、路由器等。 这是和之前一样的例子,使用它:

class PostsController extends BaseController {

    public function store()
    {
        $post = new Post;

        $post->domain_id = Domain::where('name', getCurrentSubdomain())->first()->id;

        $post->save();
    }

}

You can also create a class and a Facade for it, so you can call this class from anywhere, like Laravel does:

你还可以为它创建一个类和一个 Facade,这样你就可以从任何地方调用这个类,就像 Laravel 那样:

Helper::getCurrentSubdomain();

Or you can do the same by just creating a class and create a static function (less testable).

或者您可以通过创建一个类并创建一个静态函数(较少可测试)来做同样的事情。

回答by Heroselohim

This is working on my Laravel 4.2 right now.

这现在正在我的 Laravel 4.2 上工作。

On your routes file:

在您的路线文件中:

Route::group(['domain' => '{subdomain}.myapp.com'], function()
{
    Route::get('/', function($subdomain)
    {
        return "Your subdomain is: ".$subdomain;
    });
});

Get subdomain in your controllers or anywhere:

在您的控制器或任何地方获取子域:

$subdomain = Route::getCurrentRoute()->getParameter('subdomain');