我想从代码点火器的 URL 中删除控制器和函数名称


I want to remove Controller and function name from URL in CodeIgniter

我当前的网址是 http://myaliveidea.com/news/news/readmore/78/Hacker-drama-Mr.-Robot-is-granted-full-series-by-USA-Network

http://myaliveidea.com/news/-> 这是基本网址。

新闻 ->是控制器名称

阅读更多 -> 是函数名称

78/黑客戏剧-机器人先生-被授予-全系列-由美国网络->我的博客ID和他的标题

所以我想从 URL 中删除控制器和函数名称 喜欢这个

http://myaliveidea.com/news/78/Hacker-drama-Mr.-Robot-is-granted-full-series-by-USA-Network

查看代码点火器用户指南中的路由文档。

如果您希望

新闻控制器成为您的主控制器。您必须在路由中设置它,因此当您转到 http://myaliveidea.com 时,它将转到新闻控制器,而不显示控制器名称。我会从您的基本 url 中删除/news/并将新闻设置为路由中的默认控制器。

$route['default_controller'] = "news"; gets index from controller
$route['default_controller'] = "news/readmore";

这里还有很好的 CI 教程 https://www.youtube.com/watch?v=9hRNFgSLLAQ

您也可以在控制器中使用 _remap() 函数。这样,如果您有动态数据,则不必担心更新路线.php

https://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping

将以下代码(未完全测试)放入新闻控制器中。

/**
 * Intercept all calls to this class. 
 * 
 * @access private
 * @param string
 * @param array
 * @return boolean
 */
function _remap($method, $params)
{
    // If method exists, call that method.
    if (method_exists($this, $method) !== false) return call_user_func_array(array($this, $method), $params);
    // Method is actually a news_id and 1st parameter is title
    $sql = 'SELECT news_id FROM news WHERE news_id = ? AND title = ?';
    $news_id = $method;
    $title = (isset($params[0])) ? $params[0] : '';
    $qobj = $this->db->query($sql, array($method, $title); 
    if ($qobj->num_rows()) {
        return $this->readmore($news_id, $title);
    }
    // Non-existing method
    show_404();
}