Yii框架UrlManager规则编写:移除URL中menu-触发CHttpException求助
Hey there, let's work through this Yii URL routing issue together! The "Unable to resolve the request" error usually means your UrlManager rules aren't properly handling the new URL structure, either for parsing incoming requests or generating outgoing links. Here's how to fix it step by step:
1. Fix Your UrlManager Rules (Critical!)
Yii's UrlManager needs rules that handle both directions: generating clean URLs and parsing incoming requests to map to your actionMenu. Make sure your rules prioritize the new structure and correctly map slugs to your action.
Update your config.php urlManager section like this:
'urlManager' => array( 'urlFormat' => 'path', 'showScriptName' => false, 'rules' => array( // Rule 1: Parse incoming URLs like "example.com/burger-king" to actionMenu '<slug:[a-z0-9-]+>' => 'site/menu', // Optional: Keep old rule for backward compatibility (remove if you don't need it) 'menu-<slug:[a-z0-9-]+>' => 'site/menu', // Fallback rule for other controllers/actions (keep this last!) '<controller:\w+>/<action:\w+>' => '<controller>/<action>', ), ),
- The regex
[a-z0-9-]+ensures only valid slugs match this rule (adjust if your slugs use other characters). - Order matters! Put the specific slug rule first so Yii doesn't try to match it to a controller/action name.
2. Verify Your Action Method
Double-check that your actionMenu method in the controller correctly accepts the $slug parameter (make sure the parameter name matches the one in your URL rule):
public function actionMenu($slug) { // Your existing logic (e.g., fetch menu item by slug) $menuItem = Menu::model()->findByAttributes(['slug' => $slug]); if (!$menuItem) { throw new CHttpException(404, 'Menu item not found'); } $this->render('menu', ['model' => $menuItem]); }
If the parameter name doesn't match (e.g., you used $id instead of $slug), Yii won't be able to resolve the request.
3. Update URL Generation in Views
Make sure you're using Yii's URL helper to generate links instead of hardcoding. This ensures the new rule is used to create clean URLs without "menu-":
// Instead of hardcoding "example.com/burger-king" echo CHtml::link('Burger King', Yii::app()->createUrl('site/menu', ['slug' => 'burger-king']));
This will automatically generate example.com/burger-king instead of the old "menu-" version.
4. Check Server Rewrite Rules
If you're using path URL format, ensure your web server is forwarding all non-file/directory requests to Yii's index.php:
- Apache: Confirm your
.htaccessin the web root has this:RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php - Nginx: Add this to your server block:
location / { try_files $uri $uri/ /index.php?$args; }
A missing or incorrect rewrite rule can cause server-level 404s that Yii never gets to handle.
5. Clear Yii's Runtime Cache
Yii caches URL rules to improve performance. After updating your config, delete all files in protected/runtime/ to force Yii to load the new rules.
Once you've made these changes, test both accessing the clean URL (e.g., example.com/burger-king) and generating links from your views—this should resolve the "Unable to resolve the request" error.
内容的提问来源于stack exchange,提问作者Sami Khan

