【问题标题】:how change query string to segment url in codeigniter?如何在codeigniter中将查询字符串更改为分段url?
【发布时间】:2015-08-20 08:04:47
【问题描述】:

大家好

这是我在这里的第一个问题希望你们都能理解我的问题并帮助我..

所以这是我正在开发基于体育社区CODEIGNITER)的应用程序的问题,所以当登录用户点击在团队名称或运动页面上重定向到特定团队页面,网址如下所示

http://localhost/project/home?cat_id=MjY=&cat_type=Mg==

cat_id 和 cat_type 在base64encode() 中的位置

我想让它对 seo 友好.. 我已经尝试过这个链接

How to Convert Query String to Segment URI? 但是什么都没发生,任何人都可以帮助我 PLZ

当前网址是localhost/project/home?cat_id=MjY=&cat_type=Mg==

想要的网址localhost/project/home/team-category

提前致谢.....

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    基本上,您要寻找的是 基于 slug 的 URL (Semantic URLs)


    那么如何实现slug呢?

    会举例说明:
    URL - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/

    1) 假设您有一个产品页面,并且产品页面当然需要来自 URL 的一些数据来了解要显示的产品。
    2) 在我们使用id 查询我们的数据库之前,我们是从 URL 获取的。但是现在我们将做同样的事情(查询我们的数据库),只需将 id 替换为 slug 就可以了!
    3) 因此,在您的数据库中添加一个名为slug 的附加列。下面将是您更新的产品数据库结构(只是一个示例)。

    Columns                       Values
    
    id (int(11), PK)              1
    title (varchar(1000))         Apple iPhone 5S 16GB
    slug (varchar(1000))          apple-iphone-5S-16GB-brand-new
    price (varchar(15))           48000
    thumbnail (varchar(255))      apple-iphone-5S-16GB-brand-new.jpg
    description (text)            blah blah
    ...
    ...
    

    为此,您必须进行以下更改 -

    1) 创建以下 2 个表格

    slug_table:
    
    id (PK)   |    slug    |   category_id (FK)
    
    
    category_table:
    
    id (PK)   |  title  |  thumbnail  |  description
    


    2) config/routes.php

    $route['/products/(:any)'] = "category/index/$1";
    


    3) 模型/category_model.php

    class Category_model extends CI_Model
    {
        public function __construct()
        {
            parent::__construct();
            $this->db = $this->load->database('default',true);
        }
    
        public function get_slug($slug)
        {
            $query = $this->db->get_where('slug_table', array('slug' => $slug));
    
            if($query->num_rows() > 0)
                return $query->row();
            return false;
        }
    
        public function get_category($id)
        {
            $query = $this->db->get_where('category_table', array('id' => $id));
    
            if($query->num_rows() > 0)
                return $query->row();
            return false;
        }
    }
    


    4) 控制器/category.php

    class Category extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('category_model');
        }
    
        public function index($slug)
        {
            $sl = $this->category_model->get_slug($slug);
    
            if($sl)
            {
                 $data['category'] = $this->category_model->get_category($sl->category_id);
                 $this->load->view('category_detail', $data);
            }
            else
            {
                 // 404 Page Not Found
            }
        }
    }
    


    5) 意见/category_detail.php

    <label>Category title: <?php echo $category->title; ?></label><br>
    </label>Category description: <?php echo $category->description; ?></label>
    


    我之前也回答过slug。检查它是否有帮助。
    How to remove params from url codeigniter

    【讨论】:

    • 抱歉不能投票,因为它需要 15 名声望,所以只需将你的答案标记为正确
    猜你喜欢
    • 2017-09-29
    • 2015-02-13
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2016-10-29
    • 2016-04-24
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多