Yii2 site/page 404 error - yii2

After a couple of weeks break for other commitments, I came back to my Yii2 work, ran composer update on my current projects, & am getting 404 errors on any previously working pages in #frontend/views/site/pages.
My URL manager is...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '.p2m', // confuses bad guys
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<view:[a-zA-Z0-9-]+>/'=>'site/page',
]
],
An example of the line from my Navbar is...
['label' => 'Some Page', 'url' => ['/site/page', 'view' => 'somepage']],
Producing the URL...
http://example.com/site/page.p2m?view=somepage
Prior to today this all worked, & I'm tearing my hair out trying to makes sense of why it no longer does.
TIA, Pedro

Perhaps you've confused the names of the action?
Please show your actionPage() in SiteController. May be you have actionPages() instead?
Also you can beautify your code.
actionPage($view)
Then add in rules
'site/page/<view:[\w-]+>' => '<controller>/<action>',
And Url site/page/somepage will work.
But better way is make PageController and every page as separate action.
Also check your .htaccess and httpd.conf (if using apache) or nginx config to make sure that the server is working correctly.

Related

Yii2 internationalizating slug and controller

I'm making a yii2 site in 2 languages (for now) using yii's native i18n module, but how can I add multilanguage support for the action URLs?
For instance one of my actions is category/slug and in English, it will show as
http://example.com/category/chair
but in spanish it has to be shown as
http://example.com/categoria/silla
and so on for other languages shown in the future
right now im manually adding my routes like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<alias:\w+>' => 'site/<alias>',
'marca/<slug>' => 'site/brand',
'categoria/<slug>' => 'site/category',
'brand/<slug>' => 'site/brand',
'category/<slug>' => 'site/category',
],
],
Do i need to manually route every action to its correct controller or is it possible to add a more automated form using yii::t() function?
you will need to write your own UrlRule class, implementing yii\web\UrlRuleInterface and configure your UrlManager.
read more here.
basically it is about "translating" a given request like '/categoria/silla' to your internal url ['site/category', 'slug' => $slug, 'lang' => $lang] in your UrlRule's 'parseRequest' method. 'createUrl' is the other way round.

Yii2 -- Pretty Url -- Domain to Controller/Action with parameters

What will be the rules for my pretty url if I have the following scenario:
links like this where parameters may vary.
domain/?bt=<token>&e=<email>
or
domain/?lt=<token>&e=<email>
then should be processed in a controller/action. ie. mycontroller/get
Also, parameters should be accessible by $_GET inside the action.
the simplest way is based on the use of urlHelper
use yii\helpers\Url;
$myUrl = Url::to(['your_controller/your_action', 'bt' => 123, 'e' => 'myemail#gmail.com']);
Using the urlHelper function Url::to .. the url you need is properly formed depending of the urlManager configuration you have set in your config file
and the param a manager as show in the sample like entry in an array.
The post or get method is related to the type of metho you have in your ulr call if not other values are specified the url is formed as a get
and you can obtain the values you need in $_GET['bt'] and $_get['e']
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html
http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html
'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>',
'' => 'call-backs/get',
'unsubscribes' => 'unsubscribes/get',
],
],
#scaisEdge, thank you for answering my question. maybe my question isn't that clear but this is the solution I made for my question after a hard find of clues and tips online.
All I wanted was that when a user clicks on a link, hitting the main page/main domain, it will go to my yii project (intended to be a webservice or an API like one) then will be handled by a precise controller and action.
'' => 'call-backs/get'
the code above answers the question. Cheers.

How can I directly go to an action in the browser?

I have a custom contorller called A1Controller which has an action called actionGetdetails. How do I go to this action in the browser? I tried http://localhost/Yii2basicapp/web/a1/getdetails, but it doesn't work.
This is my setting in the web.php file
`'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'a1'],
],
],`
Yii expects camelcase actions to be rendered with dashes, so you should access it like http://localhost/Yii2basicapp/web/a1/get-details
You need to use a rest full browser add on to access the rest controller action to work as rest client for You
I use Rest Client Firefox for Firefox You can search for alternative for Your favorite browser

Url rule overrides in Yii

I am just getting into Yii and so do excuse me if this looks blindingly obvious. I am trying to setup some URL rules so that:
www.example.com/my_page & www.example.com/my_page/my_subpage
both hit the main pages controller and the content pulled from a database, then:
www.example.com/admin/pages/edit
Works from the Admin Controller. I seem to have an either or situation at present, can anyone shed some light as to where I may be going wrong?
This is what I currently have in my web config file:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
//'<key>' => 'pages/index',
'admin/<action:\w+>' => 'admin/index',
],
],
If I toggle the commenting on either line 5 or 6 then it seems to work as I expect, however having both lines uncommented the '' line takes precedent.

Url rules for module are not working

this part of yii2 configuration is kind of tricky, so i would really appreciate if anyone tells me the proper way to do it and why what i have tried is wrong..
i have a module called Admin, this module has few controllers and of course it takes the main layout for the view structures so all the links generated in the layout are available in the module as well, all seems nice but one thing, if the links are rendered in the module, all of them will have the module route in all these links.. i need them to be routed outside the module..
all the links i need them to be routed outside of the module have this pattern:
<controller>/<action>
while the module has this pattern:
admin/<controller>/<action>
so far i have tried adding an Url rule in the config like this:
'rules'=>[
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
//Also tried this: admin/<controller\w+>/<action:\w+>
]
but all of the links are still being generated in the same way.. when links are in generated in the module, they all have this pattern: admin/<controller>/<action> when they are just supposed to be something like this: #root/<controller>/<action>
Assuming you still have the default url rules, your urls should start with a leading slash e.g /site/about instead of site/about. From the docs for Url::toRoute() :
A route may be either absolute or relative. An absolute route has a leading slash (e.g. /site/index), while a relative route has none (e.g. site/index or index)
...
If the route has no leading slash (e.g. site/index), it is considered to be a route relative to the current module and will be prepended with the module's uniqueId.
Example for custom rules from a personal proyect:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'register' => 'user/registration/register',
'login' => 'user/security/login',
'logout' => 'user/security/logout',
'home' => 'site/index',
'support' => 'contact/submit',
'faq' => 'site/faq',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
This goes to the main config file in your app.