passing variables through url in DRUPAL - php

Whenever I try to pass a variable through url with the l() function like:
l(t($row['salon_name']),'admin/content/edit-salons-products-services?sid='.$row[salon_id] );
? is replaced by "%3F"
= is replaced by "%3D"
Why is this happening and how can I fix it?

Change it to: 'admin/content/edit-salons-products-services/.$row[salon_id]'.
You can access the salon id with arg(3).
You may also need to change your module's menu declaration to allow this URL.

As Finbarr said, it's often better to pass variables as path components, rather than query parameters, but query parameters are still possible with l().
Query parameters are passed into l() outside the base $path, in the $options parameter. This makes it easier to programmatically alter query values, without needing to parse a string. What you want is something like this:
l(t($row['salon_name']),'admin/content/edit-salons-products-services', array('query' => array('side' => $row['salon_id'])));

Related

Laravel 5.2 Routing with optional params

I am creating a simple product search engine in Laravel 5.2. I can use either get or post, whichever can accomplish what I'm wanting, even if I need to do some backend processing then pass the pretty URL to another method to show the products.
My parameters are
- query
- merchant
- brand
- page
- sort
All of these parameters can be used on their own or separately.
I'm wanting to use pretty URLs if at all possible.
Basically I want the URLs to look something like this:
/shop/query/shoes
/shop/query/shoes/brand/nike
/shop/query/sort/price
/shop/merchant/amazon
There can be many different routes formed by these 5 parameters, but they are all optional. So what is the best solution to making this route work how I'm wanting, without coding for every single possible route.
I'm sure I am overlooking something. I've used Zend Framework before and just use a * after shop and then I can pass anything in regardless.
If you need any other information, let me know. I appreciate any help.
Try something like this
Route::get('shop/{params?}', function(Request $request, $params = '') {
// everything after "shop/" will be in $params
// you need to add custom logic to parse and handle $params string
return $params;
})->where('params', '(([a-zA-Z0-9-_]+)\/?)+');
{params?} is Optional Parameter
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value
->where('params', ...) is Regular Expression Constraint
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained
NOTE
Make sure that you tweak (([a-zA-Z0-9-_]+)/?)+ regular expression to cover all of your cases, as this is something that I added to quickly test your examples

Access URL param from Input::get() with laravel

I have a route like this:
Route::get('demo/{system?}', 'MonitorController#demo');
I am using it like so because I would like my url to look like so:
mysite.com/demo/spain-system
Where spain-system will be the variable I need to get.
Right now, I'm getting it like this:
public function demo($systemName = null){
}
But I would like to be able to access to it as if it were a URL parameter with Input::get('system') so I can access to it from other methods or even from other controllers such as BaseController.php.
Is there any way to achieve this?
I've played around with Route::input('system') but then it doesn't work when I pass it as a get parameter (in other Ajax calls and so on)
Update
In PHP we can get URL params by using the $_GET function and laravel provides the function Input::get() to do so as well.
If there were no routes in laravel, I would make use of .htaccess rewrite rules to change this:
mysite.com/demo/?system=spain-system
To this:
mysite.com/demo/spain-system
And I could still retrieve the variable system as a GET parameter by using $_GET["system"].
That's kind of what I would expect of laravel, but it seems it is just treating it as the parameter of the demo method and not really as a URL variable.
Is there any way to keep treating it as a URL variable and at the same time use it in a pretty URL without the ?system= ?
So you actually just want to get an url like this? mysite.com/demo/spain-system instead of mysite.com/demo/?system=spain-system? Laravel provides that by default?
Look, When you want to get the router variable {system?} to be accesible you'll need to do this:
In your router:
Route::get('demo/{system}', 'MonitorController#demo');
Then you have an controller where this noods to stand in:
public function demo($system)
{
//your further system
//You are be able to access the $system variable
echo $system; //just to show the idea of it.
}
When you now go to to localhost/demo/a-system-name/, You'll see a blank page with a-system-name.
Hope this helps, because your question is abit unclear.

codeigniter rerouting url with some new additional parameters

I am using codeigniter re-route to clean up some urls.
I am aware that I can do
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
But in some cases I have to add some extra parameters to the redirect url so that I get them as a param to the method. for example
$route['product_unique_and_rare'] = "catalog/product_lookup_by_id/{HERE I WANT SOME ADDITIONAL EXTRA PARAM}";
How to do this so that I get the value in the param of the method rather than the value in uri->resegment
you can try this
$route['product_unique_and_rare/(:num)'] = "catalog/product_lookup_by_id/$1";
Get the param in product_lookup_id like this
function product_lookup_id($product_id){
/*$product_id will be the passed parameter*/
}
So, if someone goes for http://domain.com/product_unique_and_rare/23, $product_id will get the value 23.
You can hard-code the parameter too, but I believe you aren't looking for that.

Zend Framework $_REQUEST equivalent

I need to capture several parameters in a controller regardless of whether the were posted or they are in the url.
Does $this->_request->getParam('parameter') work regardless?
To make life easier and shorter code, you can use the _getParam function in your controllers:
$page = $this->_getParam('page', 1);
Note that the second function variable is the default value if the request didn't include that specific variable.
Short answer, yes.
If you are in the controller, you can access any POST of GET parameter by accessing the getParam() method like you said.
$this->getRequest()->getParam("foo") will get the parameter foo, if it is present in the URL via a get param, or in a POST. It will also get any user set parameters.
The
$this->getRequest()->getParams();
Will get several parameters regardless of the action type being sent (get or post).
$this->getRequest()->getParam('foo');
Will get you individual requested parameter.
i prefer always use short function:
$parameter = $this->_getParam('parameter');

CodeIgniter - unlimited parameters?

I am currently using CodeIgniter.
I am trying to write a function that can take an unlimited number of paramaters.
So in the controller it will be something like
function test($name, $others){
foreach($others){
//do something
}
}
and I could call it like
example.com/control/test/some name/param1/param2/param3/param4/param5...
How can I set this up?
you could also do it like this:
function foo($params=array())
{
$params=func_get_args();
//print_r($params);
}
so any url like:
site.com/controller/foo/param1/param2/param3/param4
would create an array of parameters.
You can get an associated array of URI segments with the uri_to_assoc function in the URI class. So in your controller, you might do something like this:
function test($name){
$uri_seg = $this->uri->uri_to_assoc(4);
foreach($uri_seg as $para){
// Do something with each of the URI segments passed in here after $name
}
}
It's not clear why you need an unlimited (variable) number of arguments, but you may want to consider why you need them.
You can use alternative URI semantics for certain types of arguments. Often the URI design is the path to a thing in a system. Unlimited parameters may not refer to the resource, but rather describe its attributes. Tags are a good example of this.
/chicken/red+tall+fat
Tags implement easily in CodeIgniter with a single parameter.
If the parameters refer to a node, you can consider a similar approach:
/hen-house.bunk4.topshelf/chicken
Or, if the hierarchy is important as a path, you can use the func_get_args() solution above.

Categories