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

php – Laravel路由 – 相同路由的自定义域,第1张

概述说我有theese路线: http://mylaravelapp.com/{slug}http://mylaravelapp.com/{slug}/pagehttp://mylaravelapp.com/{slug}/another/{custom_slug} 我希望从自定义域访问相同的路由,指向我的laravel应用程序的IP.像这样: http://customdomain.com/ht 说我有theese路线:

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

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

http://customdomain.com/http://customdomain.com/pagehttp://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('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,$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/pagehttp://customdomain.com/another/{custom_slug}

为此,自动:

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

Apache的另一种方法.

总结

以上是内存溢出为你收集整理的php – Laravel路由 – 相同路由的自定义域全部内容,希望文章能够帮你解决php – Laravel路由 – 相同路由的自定义域所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1246321.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存