CodeIgniter HMVC路由修改问询:自定义短URL跳转实现
Hey there! Let's tackle this CodeIgniter HMVC routing problem to get those clean website/url slugs working for your product details. Since your product URLs are configured dynamically from the backend, we can't rely on hardcoded routes like your current $route['Products'] = "Products/detail/d101productdatatest(this is url)"; — we need a dynamic approach instead.
Option 1: Use Closure Routing (Clean & Direct)
CodeIgniter supports closure-based routes, which let you run custom logic to resolve the URL. This is great for checking if the slug exists in your product database and routing it accordingly.
Steps:
- Keep your fixed routes first: Make sure all your existing static routes (like admin pages, about us, etc.) are defined before the dynamic slug route. This ensures they don't get overridden.
- Add the dynamic route at the end of
application/config/routes.php:
// Your existing fixed routes go here $route['admin'] = 'admin/dashboard'; $route['about'] = 'pages/about'; // ... other static routes // Dynamic product slug routing - MUST be last! $route['(:any)'] = function($slug) { // Get the CodeIgniter instance $CI =& get_instance(); // Load your product model (adjust the model path if using HMVC modules) $CI->load->model('products_model'); // Check if this slug exists in your product table $product = $CI->products_model->get_product_by_slug($slug); if ($product) { // Route to your existing Products/detail method with the slug return "Products/detail/{$slug}"; } else { // If no product matches, show 404 page show_404(); } };
- Update your model: Make sure you have a method like
get_product_by_slug($slug)in your products model that queries the database for a product with the matching custom URL slug.
Option 2: Use a Dedicated Short URL Controller (For Environments That Don't Support Closures)
If your server environment doesn't allow closure routes (rare, but possible), create a dedicated controller to handle the slug resolution:
1. Create the Controller
In your HMVC modules (or main controllers folder), make a Shorturl controller:
class Shorturl extends CI_Controller { public function index($slug) { // Load the product model $this->load->model('products_model'); // Check for the product slug $product = $this->products_model->get_product_by_slug($slug); if ($product) { // Load the Products module and call the detail method $this->load->module('Products'); $this->Products->detail($slug); } else { // Show 404 if no product found show_404(); } } }
2. Update Routes
Add this route at the end of application/config/routes.php:
// Fixed routes first $route['admin'] = 'admin/dashboard'; // ... other static routes // Route all unmatched URLs to the Shorturl controller $route['(:any)'] = 'Shorturl/index/$1';
Critical Notes
- Route Priority: Always place the dynamic
(:any)route last. If you put it before other routes, it will match URLs likewebsite/adminand break your existing pages. - Database Performance: For high-traffic sites, cache the product slug-to-ID mapping using CodeIgniter's caching library to avoid repeated database queries on every request.
- HMVC Module Loading: If your
Productscontroller is in a module, ensure you're loading it correctly with$this->load->module('Products')instead of the standard controller load. - 404 Handling: Never skip the
show_404()call — this ensures invalid slugs return proper 404 responses, which is good for SEO and user experience.
内容的提问来源于stack exchange,提问作者nikunj

