Hello,
currently I am trying to set up pretty url’s in my project (Yii2) but it doesn’t work like I thought.
In my config/web.php I added the ‘urlManager’
'urlManager' => [
'showScriptName' => false, // Disable index.php
'enablePrettyUrl' => true, // Disable r= routes
'enableStrictParsing' => true,
'rules' => array(
'' => 'site/index',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
I created a .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
and placed it into the web folder. Xampp should be configured correctly.
I have 2 controllers. First on is the standard controller -> SiteController. The second is a controller created by myself TemplateController.
If I type localhost/site/login or localhost/site/contact everything is working well. The defined action is getting called. But if I try to open localhost/template/test I get an error that the page couldn’t be found.
Now I added a new rule to the urlManager
'template/test' => 'template/test',
After adding this, it works. But why the action doesn’t get called without the rule? I thought the last standard rule
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
will be executet in this case and I don’t need to add an extra rule. Why does it work with the site controller? Are there any more settings I need to make?
Maybe it’s inside the controller? The SiteController has the methods behaviors and actions. My TemplateController only extends from Controller but don’t implement both methods. Just the action test.
class TemplateController extends Controller
{
public function actionTest(){
echo "test";
}
}
Maybe someone of you guys can help me. Thanks alot.