Reputation: 1883
I want my url's to have the following patter:
http://example.com/controller/view_id/title_name, like SO:
http://stackoverflow.com/question/1/title
Config.php:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'appendParams' => true,
'rules'=>array(
'user/<id:\d+>/<name:\w+>'=>array('user/view', 'caseSensitive'=>false),
'<controller>/<id:\d+>/<name:.*?>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
)
UserController:
public function actionView($id)
{
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
I am trying to redirect user/view/1
to user/1/my_username
The url user/1/username
is redirecting me to Error 400 invalid request
.
What am I missing?
Upvotes: 2
Views: 317
Reputation: 2885
if your user and question controller has action view
.Here view should be the name of action.
Try this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'appendParams' => true,
'caseSensitive'=>false,//use case sensitivity here
'rules'=>array(
'<controller>/<view_id:\d+>/<title_name:[\w -\.]+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
)
If your Url is like this
http://stackoverflow.com/question/1/title
When you hit this url.In main.php this line will read your url.
'<controller>/<view_id:\d+>/<title_name:[\w -\.]+>'=>'<controller>/view',
//view_id and title_name you can change there name.
It will work like this:
<controller>=question
<view_id:\d+>=1
<title_name:[\w -\.]+>=title
and you will redirect to <controller>
's function actionView()
automatically.You can get the url values like this $_GET['view_id']
AND $_GET['title_name']
in actionView().
You have to Do this:
public function actionView()
{
$this->render('view', array( 'model' => $this->loadModel($_GET['view_id'])));
}
Upvotes: 2