php - 在 CodeIgniter 中用别名值(文章标题)替换 URI 参数(文章 ID)

标签 php codeigniter seo codeigniter-url

我在 Code Igniter 中有以下 URL:

http://example.com/Mysite/News/id

哪里:

  • 消息: Controller 名称
  • id:用于从DB中获取相应消息的参数

这将呈现带有此 id 的新闻(也带有标题和正文等) 我需要显示(并且只显示)此别名的 url。

http://example.com/Mysite/News/news-title

我该怎么做?

最佳答案

使用 PHP is_numeric 函数 http://php.net/manual/en/function.is-numeric.php

is_numeric — Finds whether a variable is a number or a numeric string

public function news($parameter)
{
    $this->load->database();

    // If the parameter is number, fetch data with id.
    if ( is_numeric($parameter) ){        
        $news = $this->db->from('news')
                         ->where('id', $id)
                         ->get()->row_array();
    }
    // Otherwise, fetch data with title
    else{
        $news = $this->db->from('news')
                         ->where('title', $title)
                         ->get()->row_array();
    }

    // Render them
}

关于php - 在 CodeIgniter 中用别名值(文章标题)替换 URI 参数(文章 ID),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231524/

相关文章:

seo - Google Rich Snippets 每页的“事物”

php - 我是否需要为 AngularJS 的每条内容创建一个单独的页面?

ajax - AJAX 会导致我的网站跳出率很高并损害我的搜索评级吗?

php - 使用cakephp上传文件到ftp服务器?

php - 使用 htaccess 重定向/重写

codeigniter - Firebase 网络推送通知

model-view-controller - CodeIgniter 的局限性

javascript - 在 Yii2 中将 jQuery 脚本添加到单个 View

php - 为什么元素显示在我的 <body> 而不是 <head>

javascript - Form_Open 始终提交 - 通过 Ajax 提交 - CodeIgniter