简体   繁体   English

Laravel 路由域 - 跳过一些域

[英]Laravel Route Domain - skip some domain

Hi你好

My application has to work with several domains.我的应用程序必须与多个域一起使用。 But I need certain routers to be ignored by ONE specific domain (base.domain).但是我需要某些路由器被一个特定的域(base.domain)忽略

As a result, the routers must work with different domains than the base.domain因此,路由器必须使用与 base.domain 不同的域

Help please, spent a lot of time and could not do it.请帮忙,花了很多时间,做不到。

// this code doesn't need to work with "base.domain"

Route::group(['domain' => '{domain}.{ltd}'], function() {

 //some routes

});

if the domain will be "base.domain" - these are the routers you should ignore and use the other ones.如果域将是“base.domain” - 这些是您应该忽略并使用其他路由器的路由器。

I tried using middleware instead of the Route::domain, but it's fail:(我尝试使用中间件而不是 Route::domain,但它失败了:(

You can do this via middleware , create a subdomain middleware for route group and check for base domain in middleware and restrict to proceed further, something like below:您可以通过middleware执行此操作,为路由组创建一个子域中间件并检查中间件中的基本域并限制继续进行,如下所示:

In your routes/web.php在你的路线/web.php

Route::middleware(['subdomain'])->group(function(){
     //some routes for subdomains only..
});

Create a new middleware in your app and define in $routeMiddleware under app/Http/Kernel.php在你的app中新建一个中间件,在app/Http/Kernel.php下的$routeMiddleware中定义

In app/Http/Middleware/Subdomain.php在 app/Http/Middleware/Subdomain.php

public function handle($request, Closure $next){
    if($_SERVER['HTTP_HOST'] == config('app.base_domain')){
        // redirect to base domain 
        return redirect(config('app.base_domain'));
        
        //Or your can abort the request 404
        abort(404,'Not Found');

    }
    return $next($request);
}

Define base_domain variable in your config/app.phpconfig/app.php中定义base_domain变量

That's it!就是这样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM