2011-02-08 111 views
0

我對codeigniter很新穎。我知道PHP。SEO網址在CodeIgniter中給404錯誤

我該如何完成加載正確的視圖?

我的網址:/博客/這 - 是 - 我的標題

我已經告訴控制器類似

如果結束($這個 - > URI-> segment_array())不存在中DB然後將這些數據加載到某個視圖中。

我得到一個404錯誤,每次我訪問/博客/不管

什麼我看到了?

+0

給我們你正在使用的代碼,如果你想要特定的幫助 – jondavidjohn 2011-02-08 21:14:18

回答

1

除非您使用路由,否則URL始終爲404,因爲CI正在尋找名爲this-is-my-title的方法,當然這種方法不存在。

一個快速的解決辦法是把你的帖子顯示的代碼到另一個功能,編輯網址以訪問帖子說:/blog/view/the-post-title

如A路線:

$route['blog/(:any)'] = "blog/view/$1";

也可以達到什麼樣的你想,如果你想在URI留下的只是`/博客/這 - 是 - 我的標題」

1

的可能是更多的可能性:

  1. 最常見的 - mod_rewrite的不活躍
  2. htaccess的配置不正確(如果u沒有編輯它嘗試/blog/index.php/whatever)
  3. 控制器不存在或放置在錯誤的文件夾

    建議:如果你只需要改變的數據使用另一種觀點認爲在同一個控制器

    如果(東西) {

    $這個 - >負載>視圖( '無論');

    }

    別的

    { $這 - >負載>視圖( 'somethingelse'); }

    如果這些工作都沒有發佈代碼和配置.htaccess的示例,我會看看。

0

解決此問題的最佳方法是重新映射控制器。這樣,你仍然可以使用相同的控制器來做其他事情。

不需要路由!

enter code here 
<?php 
class Blog extends Controller { 
function __construct() 
{ 
    parent::__construct(); 
} 
public function _remap($method, $params = array()) 
{ 
    if (method_exists($this, $method)) 
    { 
     $this->$method(); 
    } 
    else 
    { 
     $this->show_post(); 
    } 
} 
function index() 
{ 
    // show blog front page 
    echo 'blog'; 
} 
function edit() 
{ 
    // edit blog entry 
} 
function category() 
{ 
    // list entries for this category 
} 
function show_post() 
{ 
    $url_title = $this->uri->segment(2); 
    // get the post by the url_title 
    if(NO RESULTS) 
    { 
     show_404(); 
    } 
    else 
    { 
     // show post 
    } 
} 
    } 
?>