Yii2 internationalizating slug and controller - yii2

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.

Related

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

urlManager rules not working on modules Yii2

I am trying to make some url rules in Yii 2 so I can access an action from the controller like this:
controller/action/1 -> controller/action (with a parameter)
I tried some rules but they won't work in my modules (www.example.com/midend, www.example.com/backend).
So, if I want to access www.example.com/controller/action/1 it's works just fine but if I want to access www.example.com/midend/controller/action/1 it return 404.
These are the rules for modules:
'<module:\w+>/<controller:\w+>/<action:\w+>/<id:\w+>' => '<module>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<module:\w+>/<controller:\w+>/<id:\w+>' => '<module>/<controller>',
These are the rules without modules:
'<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<id:\w+>' => '<controller>',
I already tried to replace <module:\w+> with midend.
Assuming your ids are integers, you should simply replace your rules by this one :
'<controller>/<action>/<id:\d+>' => '<controller>/<action>',
It will work for :
www.example.com/controller
www.example.com/controller/action
www.example.com/controller/action/1
www.example.com/module (assuming you have a default controller)
www.example.com/module/controller
www.example.com/module/controller/action
www.example.com/module/controller/action/1

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.

Yii2 create user friendly URL with parameters

Hi I've to create user friendly URL but when using with parameters it's not working.
Url:
Url::to(['site/index', 'id' => 1]);
url looks like :
localhost/testApplication/frontend/web/index.php/site/index?id=1
/forntend/config/main.php
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
//'showScriptName' => false,
'rules' => [
],
],
I want a output like
localhost/testApplication/frontend/web/index.php/site/index/id/1
and after that how to access id value in controller.
'rules' => [
'site/index/id/<id:\d+>' => 'site/index'
//'site/index/<id:\d+>' => 'site/index' Better solution
//'<controller>/<action>/<id:\d+>' => '<controller>/<action>' Will be applied for all controllers and actions
],
Routing Doc.
And after in your action :
public function actionIndex($id)
{
...
}
Is really strange, for me using the parameter 'id' always show errors, I needed to change the parameter to 'user_id', but in other parts of the code I could use, really don't know why, but try to rename the parameter.