Laravel路由 - 子域过滤

时间:2017-06-15 05:13:42

标签: php laravel-5.4

我正在使用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,则继续。否则重定向到相同的地址,但没有子域。

1 个答案:

答案 0 :(得分:3)

我建议使用中间件查询$ 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);
    }

}
相关问题