2013-01-20 22 views
0

會有幾個高調的鏈接,客戶集中精力,例如:如何在我的默認控制器上設置Laravel 3路線以獲得焦點頁面?

  • 聯繫我們@ domain.com/home/contact
  • 關於服務@ domain.com/home/service
  • 定價@ domain.com/home/pricing
  • 如何使用@ domain.com/home/how_it_works

類似的東西。我想從URL中隱藏控制器,所以客戶只看到/contact/,而不是/home/contact/。同樣的,/定價//家庭/定價/

我知道我可以設置一個控制器或每個專題頁面的路線,但他們會看,除了內容相同的我想從數據庫中提取,我寧願讓我的代碼保持乾爽。

我設置了以下路線:

Route::get('/about_us', '[email protected]_us'); 
Route::get('/featured_locations', '[email protected]_locations'); 

這工作得很好,但恐怕SEO麻煩的,如果我有在URL中的控制器的鏈接重複的內容。 (我不計劃上同時使用,但我已經知道做笨事。)

所以後來做出的路線這樣的:

Route::get('/about_us', '[email protected]_us'); 
Route::get('/home/about_us', function() 
{ 
    return Redirect::to('/about_us', 301); 
}); 

Route::get('/featured_locations', '[email protected]_locations'); 
Route::get('/home/featured_locations', function() 
{ 
    return Redirect::to('/featured_locations', 301); 
}); 

而且現在我有一個重定向。這感覺很愚蠢,但它似乎以我想要的方式工作。如果我使用較短的URL加載頁面,則會加載我的內容。如果我嘗試訪問更長的網址,我會重定向。

它只適用於大約8或9個特殊鏈接,所以我可以輕鬆管理路線,但我覺得必須有一個聰明的方法來做到這一點。

這是一個甚至PHP的問題,或者這是一個.htaccess/web.config的問題?

我用這個重定向方案創建了什麼。聰明人如何做到這一點?我一直在尋找兩個小時,但找不到一個術語來描述我在做什麼。

有沒有內置到laravel 4中來處理這個問題?

UPDATE:

這是我嘗試實現其中一個答案。這是不是工作,我不知道我做錯了什麼。

應用/ routes.php文件

Route::controller('home'); 

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

(你可以看到編輯歷史,如果你真的想看看一些斷碼)

現在域。COM/AboutYoudomain.com/aboutUs正在返回404但domain.com/home/AboutYoudomain.com/home/aboutUs仍在恢復,因爲他們應該。

最後編輯

我複製一個想法從PongoCMS routes.php(這是基於Laravel 3),我看到他們使用的過濾器來得到任何URI段,並嘗試建立一個CMS頁面。

See my answer below using route filters。這種新方法不要求我註冊的每個特殊的路由(好),但不會放棄重定向到規範(壞)

回答

0

我用routes.php文件和過濾器來做到這一點。我從好看PongoCMS

複製的想法

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

應用/ 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('[email protected]' . 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'); 
} 
+0

如果有人想出更好的答案,我會將其打開幾天。 – MrChrister

+0

這看起來像Laravel 3代碼,非常混亂, - 或者是來自Pongocms的? – Richard

+0

@理查德 - 你是完全正確的。看看我知道多少。我正在研究生產的東西,閱讀alpha文檔。 – MrChrister

0

在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'); 
    } 
} 
+0

這是不工作的權利離開我。我正在嘗試使用佈局,所以如果重要,我的控制器不會返回任何內容。這看起來不錯,所以我正在四處探索試圖讓它着火。 – MrChrister

+0

另外,我並沒有質疑PSR-0或PSR-1標準,但我只是通過示例進行研究,所以我從文檔中所做的更改越少越好。 (對我來說,現在) – MrChrister

+0

對不起,我無法得到這個工作。我認爲我的安裝仍然是開箱即用的。 – MrChrister

相關問題