YII urlManager rewrite singe URL with multiple parameters - php

Ok, removed my previous question and reformulated the new one, otherwise no one would understand, sorry. So here the problem that's left:
So I want to rewrite an URL to a single word which works with zero, one and two parameters, but not with 3:
Original url with 2 paramaters:
index.php?r=site/page&view=exposities&tijd=nu
urlmanager rule:
'exposities_nu'=>array('site/page', 'defaultParams' => array('view' => 'exposities', 'tijd'=>'nu')),
result:
/exposities_nu/
Now what doesn't work:
Original url with 3 parameters:
index.php?r=site/page&view=exposities&tijd=vestiging&locatie=1
urlmanager rule:
'knsm'=>array('site/page', 'defaultParams' => array('view' => 'exposities', 'tijd'=>'nu', 'locatie'=>'1')),
result:
index.php?r=site/page&view=exposities&tijd=vestiging&locatie=1
Does anyone why this last url isn't shortened to /knsm/?

Maybe you didn't set the url in the URL Manager right. See this example:
return array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'post/<id:\d+>/<title:.*?>'=>'post/view',
'posts/<tag:.*?>'=>'post/index',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
),
);
Also you have to create you URLs correct:
Yii::app()->createUrl('post/view', array(
'id'=>$this->id,
'title'=>$this->title,
));

Related

Creating Seo clean urls in yii1.1.x

i am creating seo clean url for my site .
i want to display userdetails, if user hit the url like http://localhost/2015/mysite/user/username for that i have created urlManager in yii
ex:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
array(
'class'=>'ext.sitemapgenerator.SGUrlRule',
// Optional. Default: '/sitemap'
),
'<view:(about|guidelines|faq|termsofuse|privacypolicy|help-us|spread-word)>' => 'site/page',
'contact'=>'site/contact',
'/'=>'site/index',
'<controller:\w+>/<action:\w+>/<id:\d+>/<active>/*'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
'<action:(login)>' => 'site/login',
'<action:(signup)>' => 'user/signup',
'user/<username:\w+>/*' => 'user/userdetailsseo', //user is my controller
),
)
So Now if the user search username i am redirecting to user/userdetailsseom
'/<username:\w+>/*' => 'user/userdetailsseo' working perfect for http://localhost/2015/mysite/username,
'user/<username:\w+>/*' =>'user/userdetailsseo' it's not working for http://localhost/2015/mysite/user/username i am getting page not found error...
Anyone can help me please...
Thanks..

Yii User-friendly url and keep the old get format working

I set to the main config the following url rules:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>'=>'<controller>/list',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<id:\d+>/<title>'=>'<controller>/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'\?r=<controller:\w+>/<action:\w+>' => '<controller>/<action>'
),
),
Everything is woking fine, but I also want the previous url format to keep working so I don't have to rewrite lots of urls that I don't care it to be seo-friendly:
index.php?r=controller/action&param1=value1
But it shows an error now. Is there a way to keep it working?
To my opinion the best way is to replace all old urls with your IDE regex replace option. But anyway you can do what you want this way:
Use following route rule in the urlManager config:
'rules' => [
[
'class' => 'my\namespace\UrlRule',
'pattern' => '<controller>/<action>',
'route' => '<controller>/<action>',
],
...
Extend yii\web\UrlRule with your my\namespace\UrlRule and rewrite its 'parseRequest' method so that it could use $_GET['r'] parameter value if is set:
namespace my\namespace;
class UrlRule extends \yii\web\UrlRule
{
public function parseRequest($manager, $request)
{
if ($pathInfo = \Yii::$app->request->get('r')) {
\Yii::$app->request->setPathInfo($pathInfo);
}
return parent::parseRequest($manager, $request);
}
}
You may also extend yii\web\Request instead so that it's 'getPathInfo' method could use $_GET['r'] parameter if set.

YII friendly SEO url

I am struggling with this problem and not found any suitable answer.
I want my_site/user/1 to become my_site/user/user_name
My urlManager looks like this:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'user/<username:\w+>'=>'user/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
'showScriptName' => false,
),
User Controller:
public function actionView($username)
{
$this->render('view', array(
'model' => $this->loadModel($username),
));
}
When i hit /user/admin it redirects me to actionView not to the actionAdmin from my controller and i have multiple such actions like create, delete and so on.
How can i avoid this? Is there an alternative?
Thanks.
The urlManager try's to match the url to a controller and action.
the 1st part of
'user/<username:\w+>'=>'user/view',
has matched to the user controller and the next part it looking for any string that consists of words, then sent the user to user/view
I recommend changing user/<username> to manage-user/<username> or my-account/<username>, something like that.

URL Manager - action parameter optional and link creation

I have below URL mapping in my main.php file . You can see I am expecting parameter id as an optional
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix' => '/',
'useStrictParsing' => true,
'rules'=>array(
'' => 'site/index',
'<action:(signIn|signUp|logout)>' => 'site/<action>',
'<controller:\w+>(/<id:\d+>)?'=>'<controller>/index',
'<controller:\w+>/<action:\w+>(/<id:\d+>)?'=>'<controller>/<action>',
),
),
with the above rules If I create a link using below code
<?php echo CHtml::link('×', array('index', 'id' => $this->group_id), array('class' => 'linkclose')); ?>
this creates link some thing like below , which is wrong
http://localhost/blog(/872280)?/
It should generate some thing like below
http://localhost/blog/872280/
If I don't pass the parameter in the link I mean
<?php echo CHtml::link('×', array('index'), array('class' => 'linkclose')); ?>
this generates
http://localhost/blog/index
which is fine .
But with the parameter passing it is messing up the link .. Could some one help me on this ? Thanks
URL rules in their entirety are not regular expressions. Thus, optional groups like (...)? aren't honored. You can work around this by specifying alternative rules:
'rules'=>array(
...
'<controller:\w+>/<id:\d+>'=>'<controller>/index',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),

Yii routes and creation of URLs with multiple parameters

I would like to get the URL as follow:
http://domain.com/post/1/some-titles-here
But I'm getting:
http://domain.com/post/1?title=some-titles-here
I am using this config:
'urlFormat' => 'path',
...
//'post/<id:\d+>/<title>' => 'post/view/',
'post/<id:\d+>/<title:\w+>' => 'post/view/',
'post/<id:\d+>' => 'post/view/',
...
Then to get the URL I am executing:
Yii::app()->createAbsoluteUrl('post/view', array('id' => $this->id,'title' => $this->title));
im following the third rule here:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters
The regular expression you're using to match the title is incorrect: <title:\w+> will only match single words but your title has hyphens as well.
Tuan is correct; it's matching the next rule. That's because the URL manager works its way down the rules until it finds one that matches.
Use this rule instead:
'post/<id:\d+>/<title:([A-Za-z0-9-]+)>' => 'post/view/',
That will match titles with letters, numbers, and hyphens.
This is a code that work for me:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'' => 'site/index',
'article/<id:\d+>/<alias:[-\w]+>' => 'article/view',
'article/cat/<id:\d+>-<alias:[-\w]+>' => 'category/view',
Result urls:
http://wowjp.black.dragon/article/2/test-zagolovka
http://wowjp.black.dragon/article/cat/3-345435

Categories