Yii CListView's friendly url using history.js - yii

I'm trying to make friendly urls using ClistView with history.js enabled.
right now my urls look like this
localhost/ModuleName/controllerName/PageName/index/?PageName_sort=price.desc&PageName_page=3
I'm trying to make it look nicer (?PageName_sort=price.desc&PageName_page=3) to something like this or something similar
localhost/moduleName/controllerName/PageName/sort/price/desc/page/3
i would like to remove the /index/ too
My action Controller looks like this
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('PageName',array(
'pagination'=>array(
'pageVar'=>'page'
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
My UrlManager looks like this
'PageName/page/<page:\d+>'=>'PageName/',
any idea who this can be done? or what i'm missing? Thanks

I'm not sure about getting to the url as you've specified. You can however get your url's in the following format in at least Yii 1.1 without adding additional rules to urlManager.
localhost/moduleName/controllerName/index/PageName_sort/price.desc/PageName_page/3
This is because CController::createUrl() is used to create the pagination links in CPagination::createPageUrl()

Related

Error 400 Your request is invalid in yii if i am creating link

I'm pretty new to Yii, so this might be a silly question. I must be missing something somewhere. Plz help me out.
I have just written a simple code while I'm learning Yii.
I have a spark controller which has an action that looks like this:
public function actionDownload($name){
$filecontent=file_get_contents('images/spark/'.$name);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$name");
header("Pragma: no-cache");
echo $filecontent;
exit;
}
Now I have enabled SEO friendly URLs and echo statement in the view file to display the returned download file name.
But when I go to the URL,
SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf
I get error 400 - your request is invalid.
Plz let me know if I am missing something here.
Yii keeps an eye on the params you pass into an action function. Any params you pass must match what's in $_GET.
So if you're passing $name then you need to make sure there's $_GET['name'] available.
So check your URL manager in /config/main.php, and make sure you're declaring a GET var:
'spark/download/<name:[a-z0-9]+>' => 'site/download', // Setting up $_GET['name']
Otherwise change your function to the correct param variable:
public function actionDownload($anotherName){
}
Did you create the URL correct? You should do it best with
Yii::app()->createUrl('/spark/download',array('name'=>$filename));
Make sure if you are properly supplying the parameter name ($name) in your request.
Use
SITE_NAME/index.php/spark/download/name/db5250efc9a9684ceaa25cacedef81cd.pdf
or
SITE_NAME/index.php/spark/download?name=db5250efc9a9684ceaa25cacedef81cd.pdf
instead of just SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf

Multiple GET parameters in UrlManager

I am using Yii 1.1.14.
I want to convert
http://website.com/controller/action?param1=value1&param2=value2
to
http://website.com/value1/value2
How to do this in urlManager?
First, check this to hide index.php:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
Then, the route in config.php should be like this:
'<param1:\w+>/<param2:\w+>'=>'mycontroller/myaction',
The method myaction should accept $param1 and $param2 in its constructor to be passed automatically by Yii.
This would make your app unable to look for other controllers, because that rule will accept every route with 2 words separated by /

yii search form via post instead of get

I have a simple search
public function search() {
$criteria=new CDbCriteria;
$criteria->with = array('agent');
$criteria->compare('full_name',$this->full_name,true);
if ($this->gender_id != "") {
$criteria->compare('gender_id',$this->gender_id);
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>30,
),
));
}
But I don't like that the search parameters appear in the address bar when you use the get method to search. I've changed my search widget to use the post method instead:
$form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'post',
));
But now when I hit the search button the page just refreshes instead of showing the search results, I assume I'm missing something here...
In your actionAdmin function of the controller replace $_GET by $_POST...
if(isset($_GET['Model']))
$model->attributes=$_GET['Model'];
replace $_GET in above lines by $_POST like:
if(isset($_POST['LoginLog']))
$model->attributes=$_POST['LoginLog'];
On a side note on search it is always advised to use GET instead of POST, the basic rule i use is whenever some data needs to be submitted it should be POST, whenever some data needs to be fetched it should be GET..
Update:
The main reasons I can think of i would use GET for search
1) In searches user needs the functionality to back to previous filter, which if used as get url params, is straight forward.
2) If the filter params are in url, its extremely easy to share results after certain filters..Imagine you want to share some results with a friend, would you give him instructions to filter step by step (In case of POST), or give a direct url(GET)
3) Its very easy to change params from url, imagine currently you are visiting 2nd page, but on page while displaying filters only links to next 5 pages are displayed, but you want to jump to straight 15th page results..
There will be many more advantages, I can think of these at the moment..

using url manager in yii to change url to seo friendly

How can I convert these URL to SEO friendly URL I tried Url manager in yii but didn't get the proper result is there any good tutorial regarding url manager
http://localhost/nbnd/search/city?city=new+york
http://localhost/nbnd/search/manualsearch?tosearch=Hotel+%26+Restaurants+&city=New+york&yt0=Search&searchtype=
I tried to the following setting in url manager
'<controller:\w+>/<action:\w+>/<city:\d>'=>'<controller>/<action>',
which works with url http://localhost/nbnd/search/city/city/Delhi
I wish to reduce this url to http://localhost/nbnd/search/city/Delhi
and the link I generating in my view is <?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city)); ?>
This generates link as http://localhost/nbnd/search/city?city=Delhi
How can I convert that link to like http://localhost/nbnd/search/city/Delhi
The rule should be (to remove the extra city, which is the GET parameter name):
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // not city:\d, since Delhi is a string, not digit
So the rule should be able to match the parameter name, incase you had foo/Delhi, you'd use <foo:\w+>.
And to remove the ? use appendParams of CUrlManager, (in your urlManager config):
'urlManager'=>array(
'urlFormat'=>'path',
'appendParams'=>true,
// ... more properties ...
'rules'=>array(
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>',
// ... more rules ...
)
)
When appendParams
is true, GET parameters will be appended to the path info and separate from each other using slashes.
Update: Incase you have more than one parameter being passed to the action i.e:
http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants
Use a /* at the end of the rule:
'<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>'
To get urls of form:
http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants
In Yii we can create urls dynamically For eg.
$url=$this->createUrl($route,$params);
$route='post/read'.
$params=array('id'=>100)
we would obtain the following URL:
/index.php?r=post/read&id=100
To change the URL format, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can properly understand the new URLs:
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
),
),
);
WE will obtain this
/index.php/post/read/id/100
You can refer this link for user friendly urls in yii
http://www.yiiframework.com/doc/guide/1.1/en/topics.url

Yii framework urlManager rewrite rules

I have a url that looks like this:
<controller>/<action>/param/value
and I want it to like something like this:
param/value
How can it be achieved?
I tried this rule but not sure if it's ok (controller is account and action is index).
'user/<user:.*>' => 'account/index/user/test'
If I uderstand your question correctly, you want to handle URL's like this:
mysite.domain/user/username123
And call actionIndex in AccountController with param User, which (in this case) equals "username123"
In this case you can try the rule below:
'user/<user:.*>' => 'account/index/<user>'
But maybe you will need to change the declaration if your action:
function actionIndex($user){
// code
}
I would avoid putting params into action signatures as yii doens't go about processing actions with mismatching signatures [gracefully] at all... In fact, putting $user in will bind that action to always need a $user specified and if you ever decide to change your functionality, tracking down why your action isn't being called would be harder than determining why your $_GET isn't set... I would suggest in stead of adding the $user into the signature, just do something as follows in your action.
//will always run on /user/<USER:.*>
function actionIndex(){
$user = isset($_GET['user'])?$_GET['user']:NULL;
if(!is_null($user)){
//your user specific account action..
}else{
//handle your error gracefully..
}
}
This approach lets your action be more versatile. The URL rule should be as follows:
'user/<user:.*>' => 'account/index/user/<user>' //user is defined as a get...
Hope that helps && happy coding!