How to add a dot(.) in the url in yii2? - yii2

I was trying to add a dot in my URL in yii2. I am using Url rules for this.
Right now my URL looks like this.
http://localhost:8000/user/auth_key
But i want to change it to this
http://localhost:8000/.user/auth_key
My url-rules.php file looks like this.
<?php
return [
'enablePrettyUrl' => true,
'rules' => [
'user/auth_key' => 'user/authentication'
]
];
?>
without the dot(.) the URL works fine. But I need a dot(.) in URL.
Does yii2 allow us to do this? How can I achieve this?
Any suggestions or help would be really appreciated.

Yes, Yii will treat the pattern '.user/auth_key' as plain text, you can use it directly.
The full rule would be:
return [
'enablePrettyUrl' => true,
'rules' => [
'.user/auth_key' => 'user/authentication'
]
];

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

Yii2 render database content

There is a textarea where user can edit some templates and can use variables like:
{{model.user.name}}
Application need to replace this variables with data and display HTML output.
We can write a small function that will replace variables from template with data but we need to use a template engine like Twig or Smarty.
https://github.com/yiisoft/yii2-twig
https://github.com/yiisoft/yii2-smarty
Now we can use ViewRenderer from Smarty or Twig.
$render = new ViewRenderer();
$content = $render->render($this->view,'template.twig',[
'model' => $model,
]);
But I see this error:
Unable to find template "template.twig" (looked into: .).
How can I use Smarty or Twig to render a template with the content from database in Yii2 ?
I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:
$m = new \Mustache_Engine();
echo $m->render("Hello {{model.client.firma}}",['model' => $model]);
You don't create a renderer manually, you configure your application components, like this:
[
'components' => [
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '#runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],
]
You are telling Yii that there is a renderer to handle templates with the .twig extension.
Then, all you need to do is add the .twigextension when calling render in your controller actions:
public function actionIndex()
{
return $this->render('index.twig');
Then put your twig templates in the view folders where the views normally go.
Read the documentation for the twig extension: Twig Extension for Yii 2
If you want to only use twig templates, you can avoid specifying the extension (.twig) if you set the default extension:
'components' => [
'view' => [
'defaultExtension' => 'twig',
You should create a twig component which will wrap around Twig_Environment. You can add yii\twig\Extension extension if you need. Then you should use it like this:
$renderedTemplate = Yii::$app->twig->render($template, $context);
If you really need to render a template from a string variable you may implement your own Twig_LoaderInterface. There is a Twig_Loader_String, but it's deprecated and you should not use it.

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.