You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CodeIgniter中实现含分类名称的动态URL并解决代码问题

Fixing CodeIgniter URL Routing & Model Implementation for Category/Post URLs

Hey there! Let's walk through getting your desired www.mysite.com/分类名/页面URL URL structure up and running in CodeIgniter, plus finish that get_slug model method you need.

Step 1: Configure URL Routing

First, we need to tell CodeIgniter how to map those clean URLs to your controller method. Open application/config/routes.php and add this rule (replace YourController with the actual name of your controller file):

// Route category/post URLs to your postdata method
$route['(:any)/(:any)'] = 'YourController/postdata/$1/$2';

// If you have static pages like /about or /services, add specific routes BEFORE the above rule to avoid conflicts:
// $route['about'] = 'PagesController/about';
// $route['services'] = 'PagesController/services';

Step 2: Clean Up Your Controller's postdata Method

Your current controller code has a small syntax issue with how you're storing the URI segments. Let's rewrite it to be cleaner, add validation, and properly pass data to your view:

public function postdata($category = null, $posturl = null) {
    // Make sure both category and post URL are provided
    if (empty($category) || empty($posturl)) {
        show_404(); // Show 404 if either parameter is missing
    }

    // Fetch the post from the model
    $post = $this->Post_model->get_slug($category, $posturl);

    // If no post exists with that category and slug, show 404
    if (!$post) {
        show_404();
    }

    // Pass the post data to your view
    $this->load->view('postdata', ['post' => $post]);
}

Instead of manually grabbing uri->segment, we're using method parameters directly—this is the standard CodeIgniter way to handle URI segments in controller methods, and it's more readable.

Step 3: Implement the get_slug Model Method

Assuming your database has a posts table with fields like category (storing the category name), slug (storing the page URL), title, content, etc., here's how to build the model method safely (with SQL injection protection):

public function get_slug($category, $posturl) {
    // Use CodeIgniter's query builder to avoid SQL injection
    $this->db->where('category', $category);
    $this->db->where('slug', $posturl);
    
    // Fetch a single matching record
    $query = $this->db->get('posts');

    // Return the post object if found, null otherwise
    return $query->row();
}

Key Notes:

  • The query builder automatically escapes your inputs, so you don't have to worry about SQL injection.
  • row() returns a single object (perfect for a single post page), whereas result() would return an array of multiple records.

Step 4: Ensure Clean URLs (Remove index.php)

To make sure your URLs don't include index.php, set up a .htaccess file in your project root (same folder as index.php) with this code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Also, double-check your application/config/config.php has the correct base URL set:

$config['base_url'] = 'http://www.mysite.com/';

That's it! Now when someone visits www.mysite.com/blog/my-first-post, CodeIgniter will route it to your controller's postdata method, fetch the matching post from the database, and load your view with the post data.

内容的提问来源于stack exchange,提问作者hardy thoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:31:50