I'm using Codeigniter and my page URL is here.
and I want to send this URL to the controller "adverts" and method "search" with a category parameter "cat" and id "824". To do so i use the routes like this:
$route['^en/hotels$'] = "adverts/search/cat/824";
My problem now is that i dont know how to retrieve the rewritten parameters "cat" and id "824".
When i use $this->uri->segment_array() i have the initial request URL:
array (size=2)
1 => string 'en' (length=2)
2 => string 'hotels' (length=16)
Please someone can explain me how to retrieve my parameters?
Thanks again.
you should declare a method like:
function search($cat,$id){
}
CodeIgniter will automatically bind URL parameters to method parameters.
Related
I'm working on a Cakephp project, I have one controller that made some algorithms and save information to an array. I need to call another method on that same controller and send the array as a parameter. I need to do this using redirect, just like this:
return $this->redirect(['controller' => 'Identities', 'action' => 'myMethod', $array]);
If I don't use redirect, I will not have the view of the myMethod, and I need it, but I need to have the array information on that view, something like doing $this->set to pass the array to a view.
However, I get this error:
rawurlencode() expects parameter 1 to be string, array given
How can I send the array as a parameter?
You can use query string like this.
return $this->redirect(['controller' => 'Identities', 'action' => 'myMethod', '?' => ['param1' => 'val1', 'param2' => 'val2']]);
You can provide routed elements or query string parameters.
Source
I want to rewrite my url in YII to make url seo friendly .
The URL in my current system is
http://mysite/recipe/recipedetail/1
and i want to make it like
http://mysite/recipename
how i can do it
i am trying ot use rule but they are not working my rules in config/main files are
'url.rules' => array(
'recipe/<recipename:([A-Za-z0-9-]+)>/' => 'recipe/recipedetail/<recipename:\w+>/',
),
You may try this:
'recipe/recipedetail/<id:\d+>'=>'recipe/recipedetail',
A route part of rule must not contain parameters, but must be in controller/action format:
'url.rules' => array(
'recipe/<recipename:[A-Za-z0-9-]+>/' => 'recipe/recipedetail',
),
All named parameters will be available in $_GET, so inside your controller action you can access recipename value with $_GET['recipename]`.
Additionaly, please mention that you must not wrap parameter pattern ([A-Za-z0-9-]+ in your case) in brackets.
I guess you're missing var_name in your url
'recipe/<recipename:([A-Za-z0-9-]+)>/' => 'recipe/recipedetail/var_name/<recipename:\w+>/'
get the recipename inside your controller using var_name
I have the following method that performs an Ajax request passing some dynamic value obtained from a select input.
It works fine, but the dynamic value is passed as parameter in the URL, something like states/listCities/?big_string_of_serialized_parameter .
$this->Js->event(
'change',
$this->Js->request(
array( #url
'controller' => 'states',
'action' => 'listCities'),
array( # ajax options that generates the serialized parameter
'update' => '#DealerCityId',
'data' => '$("#DealerStateId").serialize()',
'dataExpression' => true
)
)
);
I'm trying to do this in a more friendly URL way, something like states/listCities/2.
It's possible in CakePHP to generate a friendly URL like this with dynamic value from a input or is only possible passing the dynamic values as parameters?
As far as I understand it's not possible. You could pass an ID as third parameter in the URL array, but as the ID is not known at template generation time, it's not applicable in this situation. If you want to use JsHelper, you'll have to stick with the JavaScript code generation it provides.
As an alternative, you could write your own Helper: Derive it from JsHelper and override the request() method to suit your needs. You can probably take the source code of the original source code to get a head start and only modify the way the data parameter is used in code generation.
let's say i have:
http://some-domain/application/controller/action/parameter
This is somehow working in cakePHP. Now I want to now what exactly 'parameter' is. But inside the Model. How to get to this information?
I have to say that there is a formular including a 'Next' Button and I want to validate the input inside of the Model in beforeValidate(). But I have to know on which page the user was at the time of clicking the submit button. This page is 'parameter'.
Router::getParams() is available from everywhere and gives
[plugin] =>
[controller] => leads
[action] => step1
[named] => Array()
[pass] => Array()
[url] => Array(
[ext] => html
[url] => someurl/post-1
)
http://api.cakephp.org/2.3/class-Router.html#_getParams
There are two type of parameter in CakePHP, you have passed parameters and named parameters. A passed parameter is as shown in your example and will be passed as part of the url.
http://example.com/controller/action/passed_param
echo $this->params['passed'][0] // 'passed_param'
http://example.com/controller/action/name:param
echo $this->params['named']['name'] // 'param'
I would recommend getting the parameters in your controller and calling model methods with them passed through.
Such as
$this->Model->find('all', array('conditions'=>array('id'=>$this->params['passed'][0])));
As to how it's working, you will want to have a look at your routes file. In your app/config/routes.php you will find all the routing and which parts are passed.
The standard cake url format is usually as follows, as you'll see in the routes.
array('controller'=>'MyController', 'action'=>'MyAction', 'MyParam');
I can't seem to find a specific page in the book on Params, but have a google around for guides.
Model (in MVC design pattern) shouldn't have direct access to any external variables. The proper way is to pass that variable as a parameter from Controller or View:
$myModelObj->doSth($getParameter);
Using CakePHP 1.3 there are named parameters in the URL like .../name:value/...
These are used for example by pagination links .../page:2/key:date/sort:desc/...
How to generate links with HtmlHelper::link() adding/deleting such named parameters from the current URL?
Basically I want create links to add/remove/modify the category:ID named parameter in the current URL. It must not touch the URL, anchor, other named parameters, GET parameters in the URL.
Or how can I pass named parameters to HtmlHelper::link()?
link('link text', array('controller' => 'something', 'action' => 'foo', 'category' => $id))