【问题标题】:How can I setup Laravel 3 routes for pages of focus on my default controller?如何为默认控制器上的焦点页面设置 Laravel 3 路由?
【发布时间】:2013-01-20 10:31:42
【问题描述】:

会有几个高知名度的链接供客户关注,例如:

  • 联系我们@domain.com/home/contact
  • 关于服务@domain.com/home/service
  • 定价@domain.com/home/pricing
  • 工作原理@domain.com/home/how_it_works

类似的东西。我想从 URL 中隐藏 home 控制器,以便客户只能看到 /contact/,而不是 /home/contact/。与 /pricing/ 相同,而不是 /home/pricing/

我知道我可以为每个特殊页面设置一个控制器或一个路由,但除了我想从数据库中提取的内容之外,它们看起来都一样,而且我宁愿保持我的代码 DRY。

我设置了以下路线:

Route::get('/about_us', 'home@about_us');
Route::get('/featured_locations', 'home@featured_locations');

哪个效果很好,但是如果我在 URL 中与控制器的链接上有重复的内容,我担心 SEO 麻烦。 (我不计划同时使用这两种方法,但众所周知我会做一些更愚蠢的事情。)

于是做了这样的路线:

Route::get('/about_us', 'home@about_us');
Route::get('/home/about_us', function() 
{ 
    return Redirect::to('/about_us', 301);
});

Route::get('/featured_locations', 'home@featured_locations');
Route::get('/home/featured_locations', function() 
{ 
    return Redirect::to('/featured_locations', 301);
});

现在我有一个重定向。感觉很愚蠢,但它似乎正在按照我想要的方式工作。如果我以较短的 URL 加载页面,它会加载我的内容。如果我尝试访问较长的 URL,我会被重定向。

大概只有8、9个特殊链接,所以我可以轻松管理路线,但我觉得必须有一个聪明的方法来做到这一点。

这甚至是 PHP 问题,还是 .htaccess / web.config 问题?

我用这个重定向方案创造了什么。聪明人是怎么做到的?我一直在寻找两个小时,但我找不到一个术语来描述我在做什么。

laravel 4 中是否有内置的东西可以处理这个问题?

更新:

这是我尝试实现的答案之一。这是不工作,我不知道我做错了什么。

应用程序/routes.php

Route::controller('home');

Route::controller('Home_Controller', '/');

(如果您真的想查看一些损坏的代码,可以查看编辑历史记录)

现在 domain.com/AboutYou 和 domain.com/aboutUs 正在返回 404。但是 domain.com/home/AboutYou并且 domain.com/home/aboutUs 仍在正常返回。

最终编辑

我从PongoCMS routes.php(基于 Laravel 3)复制了一个想法,我看到他们使用过滤器来获取任何 URI 段并尝试创建一个 CMS 页面。

See my answer below using route filters。这种新方法不需要我注册每条特殊路线(好),但确实放弃了对规范的重定向(坏)

【问题讨论】:

    标签: php redirect seo url-routing laravel


    【解决方案1】:

    把这个放到routes.php中:

    Route::controller('HomeController', '/');
    

    这告诉你 HomeController 路由到网站的根目录。然后,您可以从 HomeController 访问任何功能。只要确保在它前面加上正确的动词即可。请记住,laravel 遵循 PSR-0 和 PSR-1 标准,所以方法是驼峰式的。所以你会有类似的东西:

    domain.com/aboutUs
    

    在 HomeController 中:

    <?php
    
    class HomeController extends BaseController 
    {
        public function getAboutUs()
        {
            return View::make('home.aboutus');
        }
    }
    

    【讨论】:

    • 这对我不起作用。我正在尝试使用布局,所以如果这很重要,我的控制器不会返回任何内容。这看起来不错,所以我正在四处寻找让它开火。
    • 另外,我对 PSR-0 或 PSR-1 标准没有异议,但我只是在研究示例,因此我对文档所做的更改越少越好。 (对我来说,暂时)
    • 对不起,我根本无法让它工作。我认为我的安装仍然是开箱即用的。
    • 我有点晚了,但参数在 Route::controller 上倒退了。第一个参数是你的映射,第二个是控制器以 RESTful 方式处理映射。
    【解决方案2】:

    我使用 routes.php 和过滤器来做到这一点。我从漂亮的 PongoCMS 中复制了这个想法

    https://github.com/redbaron76/PongoCMS-Laravel-cms-bundle/blob/master/routes.php

    application/routes.php

    // automatically route all the items in the home controller
    
    Route::controller('home');
    
    // this is my last route, so it is a catch all.  filter it
    
    Route::get('(.*)', array('as' => 'layouts.locations', 'before' => 'checkWithHome', function() {}));
    
    Route::filter('checkWithHome', function()
    {
        // if the view isn't a route already, then see if it is a view on the 
        // home controller.  If not, then 404
        $response = Controller::call('home@' . URI::segment(1));
        if ( ! $response )
        {
            //didn't find it
            return Response::error('404');
        }
        else
        {
            return $response;
        }
    
    });
    

    我看到的主要问题是过滤器基本上会加载所有成功的页面两次。我没有在文档中看到可以检测页面是否存在的方法。我可能会写一个库来做到这一点。

    当然,在这个最终版本中,如果我确实找到了一些东西,我可以将它转储到页面上并停止处理路线。这样我只加载一次所有资源。

    应用程序/控制器/home.php

    public function get_aboutUs()
    {
            $this->view_data['page_title'] = 'About Us';
            $this->view_data['page_content'] = 'About Us';
    
            $this->layout->nest('content', 'home.simplepage', $this->view_data);
    }
    
    public function get_featured_locations()
    {
            $this->view_data['page_title'] = 'Featured Locations';
            $this->view_data['page_content'] = 'Featured properties shown here in a pretty row';
    
            $this->layout->nest('content', 'home.simplepage', $this->view_data);
    }
    
    public function get_AboutYou()
    {
        //works when I return a view as use a layout
        return View::make('home.index');
    }
    

    【讨论】:

    • 我会把它打开几天,以防有人提出更好的答案。
    • 这看起来像 Laravel 3 的代码,非常混乱,——还是来自 Pongocms?
    • @Richard - 你完全正确。看看我知道的有多少。我正在阅读 alpha 文档进行生产工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2015-05-16
    • 1970-01-01
    • 2015-01-02
    • 2013-01-02
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多