Reputation: 16181
In Yii2, by default, this kind of URL: /foo-bar will be translated to this action:
public function actionFooBar() {
// ...
}
What if I want the same URL to contain a number, like this: /foo-bar-3 ?
I tried actionFooBar3()
and actionFooBar_3
- no luck, 404 is thrown when I access /foo-bar-3.
Ideas?
UPDATE:
So, the URL threw 404 because there was a typo in it, which I failed to see after too-many hours of work. Don't be a workaholic. Also, I upvote the 3 answers below, as all seem to be valid and might be helpful to others.
Upvotes: 2
Views: 1035
Reputation: 675
public function actionFooBar3()
{
echo "HI";
exit;
}
And yes please check you have given access rules permission in behaviors method of controller. Sometimes it can be a problem.
Upvotes: 1
Reputation: 10548
It is working for me. I Can Access It. Example: https://myYii2.com/foo-bar-3
public function actionFooBar3() {
echo "asd";die;
}
web.php
Check whether enablePrettyUrl
is set to true
;
'urlManager' => [
'enablePrettyUrl' => true,
],
Error: 404
showing means, you have set some rules for controller actions.
Upvotes: 1
Reputation: 5332
I think, you must create new rule for routing, if you wish implement this. In Yii docs said:
Turn the first letter in each word of the action ID into upper case
but number not have upper equivalent. If you define action name as actionFooBar3
- this action is available from url foo-bar3
Upvotes: 1