【问题标题】:Laravel Routing - Subdomain filteringLaravel 路由 - 子域过滤
【发布时间】:2017-06-15 05:13:41
【问题描述】:

我正在使用Laravel 5.4,我想过滤子域。

web.php

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

    if ($city does not exist in database) {rediret to localhost};
    Route::get('/', 'HomeController@home');

});

我想要什么

如果subdomain 存在于数据库中,则继续。否则重定向到相同的地址,但没有子域。

【问题讨论】:

    标签: php laravel-5.4


    【解决方案1】:

    我建议使用中间件来询问 $request URL 并进行相应的重定向,就像 RedirectIfAuthenticated 中间件一样。

    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Support\Facades\Auth;
    
    class CheckSubdomain
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next, $guard = null)
        {
            // check $request->url() here...
            if ($notInDatabase) {
                return redirect()->route('/somewhere');
            }
            return $next($request);
        }
    
    }
    

    【讨论】:

    • 我喜欢middleware 的想法。我设法做了一些事情,但是当浏览器中的return redirect()->route('/somewhere') url 保持不变时。如果我把hello.example.comhello 不是一个城市,我很好重定向但url 仍然是hello.example.com 而不是example.com
    • 我试过if($city->isEmpty()) { $newURL = 'example.com'; header('Location: '.$newURL); },但我得到ErrorException in VerifyCsrfToken.php line 156: Trying to get property of non-object
    • 在这里检查一些答案,可能会让你跑起来:stackoverflow.com/questions/15643718/…
    猜你喜欢
    • 1970-01-01
    • 2014-07-17
    • 2016-07-28
    • 2018-03-01
    • 2020-01-04
    • 2016-09-18
    • 2016-09-25
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多