Action name in my controller is actionMyEvents. When I try mysite.com/mycontroller/my-events I get 404 not found. Please help!
This is not the normal behavior of Yii Framework. In the default configuration, you have to call yout action like this:
mysite.com?r=mycontroller/my-events
To use clean URL like this, you have to configure your web server (probably Apache) rewrite engine, and do something like this in your config file:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
Related
After uncomment enablePrettyUrl get 404 error
For example:
The requested URL /site/index was not found on this server.
Apache/2.4.7 (Ubuntu) Server at yiibasic.com Port 80
When I comment it again, everything is working
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
]
Can't understand what the problem is.
.htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
The (Yii2) URL manager supports two URL formats:
the default URL format;
the pretty URL format.
By default 'enablePrettyUrl' is set to false this mean that the url have the format
your/index.php?r=post%2Fview&id=100
If you uncomment 'enablePrettyUrl' => true,
'components' => [
'urlManager' => [
//'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
]
you enable the format for prettyUrl like:
/index.php/post/100
like as you easy see the two format are different so an explict call based on a format return a 404 error .. when you change the urlManager format enabling or disabling pretty url ..
you can call the url avoiding this problem using urlHelper
use yii\helpers\Url;
echo Url::to(['post/view', 'id' => 100]);
with this helper Yii2 create the proper url according on the pretty url enabling state in use
You can find a brief guide on yii2 routing here http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
After adding .htaccess file in your ./web folder (if using basic template ), you must then add some rules in the urlManager section of your web.php config file...see below
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
I've made a REST API with the Yii2 basic template. I've made some changes to the webconfig so I could use links like web/users and web/users/1. It works fine but I couldn't access the web/index anymore. So i've added some more rules to the UrlManager.
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
//basic/web
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:[\d]+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
//basic/web/users
['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car']],
//basic/web/countries
//Id is een string, vandaar de tokens
['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
],
],
With the above code I can acces web/index. I could also access web/users to get a list with users. but I can't acces web/users/1. it gives an 404 error.
edit:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
//basic/web
/*
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:[\d]+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
*/
//basic/web/users
['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car', 'site']],
//basic/web/countries
//Id is een string, vandaar de tokens
['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
],
],
I've changed it like this. Now I can acces web/sites because of pluralize.
But if I go to web/sites/about for example, it will give an 404 error again. So it's not the best solution
I think it won't be easy to maintain a REST API and HTML webpages within the same config file. For example cookies and session usually are disabled with REST as recommended by its stateless nature while you may need both to authenticate a user within a classic HTML page.
I would recommend modifying your app structure and having your REST API implemented as an independent module with its own routing configuration and maybe its own Entry Script without missing with the web entry.
A better structure may look like this : (from the tutorial linked below)
+ web
+ config
+ controllers
...
+ api
+ config
+ modules
+ v1
+ controllers
.htaccess
index.php
Then you'll be able to open your HTML files within web/sites/about while retrieving your resources within api/users/1. Each entry may host its own controllers and own models or they may both share #app/models. Check this step by step tutorial to see how to implement it :
Creating a REST API for
Yii2-basic-template
- by Joachim Werner
You may also check api and auth folders in this app where both are independent REST APIs.
Instead of have:
http://www.example.com/affiliate?ref=123456546546
I prefer to have
http://www.example.com/affiliate/ref/123456546546
Is it possible to pass GET param without changing .htaccess and keep my desired structure ?
try to add an rule to your UrlManager I guess your controller is affiliate and your action index
'urlManager' => [
...
'rules' => [
'<controller:(affiliate)>/ref/<ref:\d+>' => '<controller>/index',
]
...
]
I guess if you set custom rules you also have to add the following so that the default url's are still working.
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
This is my module "auth"
-auth/
-controllers/
---AuthController.php
in controller I have action "register"
In layout I create route url:
$this->createUrl('auth/auth/register')
Rules are in config/main.php
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'register' => 'auth/auth/register',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
When I'm in mySite/index.php register url looks well: mySite/register. Url works and view is visible. When I'm this view mySite/register here starts a problem. Register url is mySite/auth/auth/auth/register - of course i see 404 error.
Why it apeears and how to solve this problem?
From the API page for createUrl
If the controller belongs to a module, the module ID will be prefixed
to the route. (If you do not want the module ID prefix, the route
should start with a slash '/'.)
Thus your code should read
$this->createUrl('/auth/auth/register')
I have this url
http://example.com/index.php/controller_name/action_name?queryString=123
This url is working fine but when I am trying to use the queryString like in the old version of Yii
http://example.com/index.php/controller_name/action_name/queryString/123
I get an "unable to solve request" error.
I 've already enable prettyurl in my config file and the following url is working
http://example.com/index.php/controller_name/action_name.
My config looks like :
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'module/<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
],
],
What am I missing?
Unfortunately this feature didn't migrate to Yii2, you can still define such rules manually
'books/view/queryString/<queryString:\w+>' => 'books/view',
Link on github with this issue
Because of many client API and Oauth servers don't work without encode
Sam Dark answer