Laravel路由-相同路由的自定义域


Laravel routing - custom domain for same routes

假设我有以下路线:

http://mylaravelapp.com/{slug}
http://mylaravelapp.com/{slug}/page
http://mylaravelapp.com/{slug}/another/{custom_slug}

我希望从自定义域访问相同的路由,指向我的laravel应用程序的IP。像这样:

http://customdomain.com/
http://customdomain.com/page
http://customdomain.com/another/{custom_slug}

这是如何实现的最佳方式?

我当前的丑陋方式

我已经制定了自己的解决方案,这是相当丑陋的。它涉及到大量的重复代码和一个讨厌的控制器。我就是这样做到的:

routes.php

/**
 * Serving from my app:
 */
    Route::get('/{slug}', ['uses' => 'MyController@show', 'as' => 'page']);
    Route::get('/{slug}/page', ['uses' => 'MyController@page', 'as' => 'page.page']);
    Route::get('/{slug}/another/{custom_slug}', ['uses' => 'MyController@another', 'as' => 'page.another']);
/**
 * Serving from custom domain
 */
    Route::group(['domain' => '{custom_domain}.{tld}'], function($domain) {
        Route::get('/', ['uses' => 'MyController@show', 'as' => 'page.customdomain']);
        Route::get('/page', ['uses' => 'MyController@page']);
        Route::get('/another/{custom_slug}', ['uses' => 'MyController@another']);
    });

MyController.php

class MyController extends Controller {
    /**
     * Find by slug or custom domain
     * 
     * @return [type]             [description]
     */
    public function findBySlugOrDomain($domain, $domain_tld, $slug) {
        if($domain && $domain_tld) {
            /**
             * Find by custom domain name
             */
                $page = $this->page->findByDomain($domain.'.'.$domain_tld)->firstOrFail();
        } else {
            /**
             * Find by slug (no custom domain)
             * @var [type]
             */
                $page = $this->page->findBySlugOrFail($slug);
        }
        return $page;
    }
    /**
     * Display the specified resource.
     */
    public function show($domain = null, $domain_tld = null, $slug = null, $type = 'home', $custom_slug = null)
    {
        /**
         * Cases
         */
            if(str_contains(config('app.url'), $domain . '.' . $domain_tld)) {
                /**
                 * Incoming request to HOME (i.e. http://mylaravelapp.com/)
                 */
                    return app('App'Http'Controllers'HomeController')->index();
            } elseif($domain && !$domain_tld) {
                /**
                 * Request to page on http://mylaravelapp.com/{slug}/page
                 */
                    $slug = $domain;
                    $domain = null;
                    $domain_tld = null;
            } else if($domain && $domain_tld && !$slug) {
                /**
                 * Request to page with slug on http://mylaravelapp.com/{slug}/another/{custom_slug}
                 */
                    $slug = $domain;
                    $custom_slug = $domain_tld;
                    $domain = null;
                    $domain_tld = null;
            } else if($domain && $domain_tld && $slug) {
                /**
                 * Request to page on http://customdomain.com/
                 */

            } else if($domain && $domain_tld && $slug) {
                /**
                 * Request to page with slug on http://customdomain.com/another/{custom_slug}
                 */
                    $custom_slug = $slug;
            }
        $page = $this->findBySlugOrDomain($domain, $domain_tld, $slug);
        switch ($type) {
            case 'page':
                return view('page.page', compact('page'));
            break;
            case 'another':
                $anotherPage = $page->another()->whereSlug($custom_slug)->firstOrFail();
                return view('page.another', compact('page', 'anotherPage'));
            break;
        }
        return view('page.home', compact('page', 'js_variables'));
    }
    /**
     * Page: page
     * 
     * http://mylaravelapp.com/{slug}/page
     * 
     * http://customdomain.com/page
     */
        public function showPage($domain = null, $domain_tld = null, $slug = null) {
            return $this->show($domain, $domain_tld, $slug, 'gallery');
        } 
    /**
     * Page: another
     * 
     * http://mylaravelapp.com/{slug}/another/{custom_slug}
     * 
     * http://customdomain.com/another/{custom_slug}
     */
        public function showAnother($domain = null, $domain_tld = null, $slug = null, $custom_slug = null) {
            return $this->show($domain, $domain_tld, $slug, 'page', $custom_slug);
        }
}

这种方式的局限性:

  • 大量重复代码
  • 每次添加新路线时,我都需要更新两次
  • 长且易于理解的控制器
  • 控制器有很多新的复杂性,如果我们需要的话,比如URL中的两个自定义插件(http://mylaravelapp.com/{slug}/another/{custom_slug}/{third_slug}

如果应用程序相同,则可以使用服务器虚拟主机。

在Ubuntu+Nginx中,你可以使用这个食谱来指导你。

你既可以制作两个虚拟主机,也可以添加,例如在Nginx中,这种重定向:

server {
        #implemented by default, change if you need different ip or port
        #listen *:80 | *:8000;
        server_name customdomain.com;
        return 301 $scheme://mylaravelapp.com$request_uri;
}

哪个会改变:

http://customdomain.com/
http://customdomain.com/page
http://customdomain.com/another/{custom_slug}

对此,自动:

http://mylaravelapp.com/
http://mylaravelapp.com/page
http://mylaravelapp.com/another/{custom_slug}

Apache的另一种方法。