繁体   English   中英

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

[英]Laravel routing - custom domain for same routes

说我有theese路线:

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中说两个自定义slu(( http://mylaravelapp.com/{slug}/another/{custom_slug}/{third_slug} slug http://mylaravelapp.com/{slug}/another/{custom_slug}/{third_slug} / 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的另一种方法。

暂无
暂无

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

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