Yii2 does not create nice url - parameter - url

I use the URL manager in Yii2 to create nice urls, which works, as long as there are no parameters on the url.
I set up the following config:
urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
Using
Url::to(array('crtl/action', 'paramx' => 'computer:net', 'paramy' => 'abc')) results in the following url:
http://localhost/crtl/action?paramx=computer:net&paramy=abc
But what I need is the following:
http://localhost/crtl/action/paramx/computer:net/paramy/abc
How can I prettify the url paramters to?

If your argument is number then rule of URL manager will be:
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>'
If your argument is text then rule of URL manager will be:
'<controller:\w+>/<action:\w+>/<name:\w+>' => '<controller>/<action>'

In case we declare pretty url, if we need result followed by '/' then we also need to define url rules for routing
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>/<id:\w+>/<ids:\w+>' => '<controller>/<action>',
],
],
So we can call this routing as :
<a href="<?php echo \yii\helpers\Url::base(true)."/site/testing/5/8"?>">
<a href="<?php echo \yii\helpers\Url::to(['site/testing','paramx'=>'x1','paramy'=>'y1'])?>">
In first case we had created the url as we want.
In second case we had used url:to for routing as we can see we had provided paramX and param y in parameter.
For both of this,the result will be,
public function actionTesting() {
print_r(Yii::$app->request->getQueryParams());
die();
}//will get the query params that we had sent
Output will be:Array ( [paramx] => x1 [paramy] => y1 ) ;
As the case you are asking is not the url routing pattern for yii2,this is the preferred way for yii2

Related

CakePHP 3 routing dynamic url

I have an eCommerce website and need to create some custom URLs. These URLs will be generated dynamically. The structure will be
Example URL: http://website.com/categoryname/product-title
Here the category name is dynamic. It will change for each product, it can be
website.com/buttons/CP1 or
website.com/ribbons/red so on
In any case these URLs should be redirected to http://website.com/products/productdetail/product-title
I tried the following script:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
['type' => '\b(?:(?!admin)\w)+\b'],
[
'slug' => '[A-Za-z-]+',
'pass' => [
'slug'
]
]
);
['type' => '\b(?:(?!admin)\w)+\b'] is to exclude the other controllers. In this case 'Admin'. This works but the parameters passed through the URL (product-title) is not getting in the controller function(productDetail)
Any help will be appreciated.
It is fixed. Updated script below.
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'pass' => ['slug']
]);
According to the API the connect() function only accepts 3 parameters and you are actually passing 4. See Cake Routing Router - Connect
I have not tested this at all, but I think you should re-write it like this:
$routes->connect(
'/:type/:slug/*',
['controller' => 'Products','action' => 'productDetail'],
[
'type' => '\b(?:(?!admin)\w)+\b',
'slug' => '[A-Za-z-]+',
'pass' => ['slug']
]
);

Where can I get the route action in zend framework

This is my module.config.php
return [
'router' => [
'routes' => [
'home' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/',
'defaults' => [
'controller' => 'rotation',
'action' => 'add',
],
],
],
And this is my add.phtml.
<?php
$form = $this->form;
$form->setAttribute('action',
$this->url('home/default', //your route name ...
array('controller'=>'rotation', 'action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('profilename'));
echo $this->form()->closeTag();
In line $this->url('home/default', //your route name ...
array('controller'=>'rotation', 'action' => 'add'))); I get a error. It doesn't display anything but when I erased that line it displays the textboxes.
My question is the url I put inside the code is correct or wrong? Thanks
For one thing to generate that route url with url helper you don't need pass in any parameters, because you've specified them in the defaults under options. Also since it's a literal I don't think you could even change the action (it's not variable for this type of route).
Sidenote: you don't need to pass in the whole route class name, because ZF2 already has it registered under Literal (case-insensitive), so you can shorten the value of type key.
$this->url('home') should return the result you're expecting.

Yii url manager only ID in url

i have in YII for example domain: www.example.com/product-url-1234
how to set rules in urlManager and how to use controller/action/id
If you want to use this url www.example.eu/product-url-1234
and suppose 'index' is the name of the action of the 'user' controller that will handle the request
Then create a rule like
'<id:.*?>'=>'user/index'
Now if you will use Yii::app()->createUrl('user/index',array('id'=>'product-url-1234'))
then it will give you the desired result.
And if you Visit this Url, User/Index will handle the request.
You are trying forward all request to one controller at root level.
I Assumed that all your request are redirected product/view route
in your config go to URL section,
'(.*)'=>'product/view'
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'(.*)' => 'product/view',
'post/<id:\d+>/<title:.*?>' => 'post/view',
'posts/<tag:.*?>' => 'post/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
that means you are capturing all your root requests to product controllers view action
Where you can get www.example.eu/product-url-1234 using
Yii::app()->request->requestUri in product/view
but this is not good way to capture all your incoming request to a single controller, the better way would be as follows.
www.example.eu/product/product-url-1234
then you have to change the configuration
'product/<product:.*?>'=>'product/view'
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'product/<product:.*?>' => 'product/view',
'post/<id:\d+>/<title:.*?>' => 'post/view',
'posts/<tag:.*?>' => 'post/index',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
in your view action, you can get the url through $_GET['product']

Adding parameters to URL in ZF2

I am trying to construct url which looks like this:
abc.com/folder?user_id=1&category=v
Followed the suggestion given in this link:
How can you add query parameters in the ZF2 url view helper
Initially, it throws up this error
Query route deprecated as of ZF 2.1.4; use the "query" option of the HTTP router\'s assembling method instead
Following suggestion, I used similar to
$name = 'index/article';
$params = ['article_id' => $articleId];
$options = [
'query' => ['param' => 'value'],
];
$this->url($name, $params, $options);
Now, I am getting syntax error saying,
Parse error: syntax error, unexpected '[' in /var/www/test/module/Dashboard/view/dashboard/dashboard/product.phtml on line 3
My module.config.php is configured like this:
'browse_test_case' => array(
'type' => 'Literal',
'options' => array(
'route' => '/browse-case[/:productId]',
'defaults' => array(
'controller' => 'Test\Controller\Browse',
'action' => 'browse-test-case',
),
),
'may_terminate' => true,
'child_routes' => array(
'query' => array(
'type' => 'Query',
),
),
),
Any Idea, please help!
Your url view helper call is wrong. First parameter is the name of a defined route, second parameter is an array with your route parameters. Your array syntax should also be correct. e.g.
array("param1" => "value1", "param2" => "value2")
In your example the correct url view helper call should be something like this:
$this->url('browse_test_case', array('productId' => '1'));
...in which "1" could be an database table row identifier, for example.
The Query child-route in your route definition, allows you to use also other url paramaters, than specified within the route. But each of these child-routes start with "/browse-case[/:productId]".
There you find the reference example of ZF2:
http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.url.html#zend-view-helpers-initial-url

Url routing for custom controller in yii

I'm new in yii framework and have a problem with url routing.
I have one controller - StaticPage and actions index (default) and send.
Thats my config:
'urlManager' => array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'call' => 'staticPage/index',
'call/send' => 'staticPage/send'
),
),
When i try set pattern like this 'call/<_a>' => 'staticPage/<_a>' i get 404 error, why ?
Always put the more specific rules on top. After a rule matches all following rules will not be checked anymore. That means in your case, if you try the URL /call/send, the first rule will match and route to staticPage/index.
If you want to add 'call/<_a>' => 'staticPage/<_a>' make this the first rule and remove the 'call/send' rule.
This works for me:
'call' => 'staticPage/index',
'call/<action:\w+>' => 'staticPage/<action>',
//or 'call/<action:(send|abc|something)>' => 'staticPage/<action>',

Resources