加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – Laravel路由 – 相同路由的自定义域

发布时间:2020-12-14 19:36:57 所属栏目:大数据 来源:网络整理
导读:说我有theese路线: http://mylaravelapp.com/{slug}http://mylaravelapp.com/{slug}/pagehttp://mylaravelapp.com/{slug}/another/{custom_slug} 我希望从自定义域访问相同的路由,指向我的laravel应用程序的IP.像这样: http://customdomain.com/http://cus
说我有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('/','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('AppHttpControllersHomeController')->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,$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','js_variables'));
    }

    /**
     * Page: page
     * 
     * http://mylaravelapp.com/{slug}/page
     * 
     * http://customdomain.com/page
     */

        public function showPage($domain = null,$slug = null) {
            return $this->show($domain,$slug,'gallery');
        } 

    /**
     * Page: another
     * 
     * http://mylaravelapp.com/{slug}/another/{custom_slug}
     * 
     * http://customdomain.com/another/{custom_slug}
     */

        public function showAnother($domain = null,$custom_slug = null) {
            return $this->show($domain,'page',$custom_slug);
        }
}

这种方式的局限性:

>很多重复代码
>每次添加新路线时,我都需要更新两次
>长而且不易理解的控制器
>如果我们需要,控制器有很多新的复杂性,请说明URL中的两个自定义slu((http://mylaravelapp.com/{slug}/another/{custom_slug}/{third_slug})

解决方法

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

在Ubuntu Nginx中,您可以使用this recipe来指导您.

你可以制作两个虚拟主机或添加,例如在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的另一种方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读