How to write url address with parameter - magento

how to write url address with parameter like this
www.example.com/param1/var1/param2/var2
and next get it with :
$param1 = Mage::app()->getRequest()->getParam('param1');
$param2 = Mage::app()->getRequest()->getParam('param2');

The default router expects that the first 3 parts in the url to be module, controller, action and then the rest of the parameters are treated as GET parameters.
You can generates such an url like this:
Mage::getUrl('module/controller/action', array('param1'=>'var1', 'param2'=>'var2'))

check here for full deails on this :
you need to do something like this.
http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters
Mage::getUrl('cms/page/view', array('id' => 1));
// http://www.example.com/cms/page/view/id/1
Also your code to get these values from url is correct.

Related

Codeigniter get parameters from url

I have a url "http://localhost/codeigniter/index.php/manual_export/". I need to get last segment from the url which is suppose to be a id. So for example "http://localhost/codeigniter/index.php/manual_export/2". I need to get the last segment which is "2".
I tired to use following code:
$id = end($this->uri->segment_array());
This works when I don't add "2" to the url and gives me "manual_export". However when I pass the id to the url I get an error "The page you requested was not found.". I think this is to do with routing. How can I fix this error.
the other way to do it is by defining a route, it will then be converted to a param
so for example if your controller is called manual_export and the method is getrecord
in the file application/routes.php
$route['manual_export/(:any)'] = "manual_export/getrecord/$1";
in your controller manual_export
function getrecord($id){ // etc etc }
You should use:
$this->uri->segment(n);//in your case n == 2 count starts just after index.php
Docs.

Laravel pass parameters to route/action

If I want to construct this url: /categories/5/update/?hidden=1 how could I pass both {id} param and hidden param (as GET) ?
My route is:
Route::get('categories/{id}/update', 'CategoryController#update');
I don't want to make a form and put it as POST because I have a number of buttons which simply hides/shows/removes a category and dont want to make a lot of forms for simple actions, although it has nothing to do with the question
I'm just a little bit confused, because it seems like action('CategoryController#update', [$id, 'hidden' => 1]) constructs the right URL but I got no idea how it's distinguished that the first one ($id) must be in URL and the second is a GET param
You may also try this to generate the URL:
$action = action('CategoryController#update', [id => $id]) . '?hidden=1';
Also, query string could be passed with any route even without mentioning about that in Route declaration.

How to get a url parameter in Magento controller?

Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
I know I can split the url on "/" to get the value, but I'd prefer a single function.
This doesn't work:
$id = $this->getRequest()->getParam('id');
It only works if I use http://example.com/path/action?id=123
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
So when you call
http://example.com/path/action/id/123
The word path is your front name, action is your controller name, and id is your action method. After these three methods, you can use getParam to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
You may also use the getParams method to grab an array of parameters
$this->getRequest()->getParams()
If your url is the following structure: http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/
then use:
echo $this->getRequest()->getParam('order_id'); // output is 1795
If you want to get All Url Value or Parameter value than use below code.
var_dump($this->getRequest()->getParams());
If your url is like this: http://magentoo.blogspot.com/magentooo/userId=21
then use this to get the value of url
echo $_GET['userId'];
If you want more info about this click here.
If it's a Magento module, you can use the Varien Object getter. If it's for your own module controller, you may want to use the register method.
Source: http://www.vjtemplates.com/blog/magento/register-and-registry

Can an empty URL parameter be passed to my controller?

Let's say my controller function is expecting 2 parameters: page_name, and user_name
The URL would be in the format http://mysite.com/controller_name/function_name/page_name/user_name
Assuming that sometimes I can have a blank user_name, and other times I can have blank page_name, can I pass a blank page_name by loading this URL?
http://mysite.com/controller_name/function_name//user_name
If the controller function is:
function function_name($page_name="default", $user_name=null)
...
Would the $page_name value be "default" for the 2nd URL stated above?
You can't. Server will simply ignore the extra slash.
Since both parameters are optional, you should use request parameters.
http://mysite.com/controller_name/function_name?page_name=p1&user_name=u1
And in your controller, use $this->input->get('page_name') and $this->input->get('user_name') to get the value and check if the values are empty.
I know this is an old question but the solution I came up with is simple. It goes something like this:
$this->input->get('limit') ? $this->input->get('limit') : 20;

Codeigniter Route to accept dynamic values

I am trying to create a router which will take a dynamic value and forward it to the actual route. In normal case it would be like
$route['login'] = 'auth/login';
It is possible to catch a parameter before the login in the above parameter and pass it to as the first parameter to the actual route ? like
$route['^(.+)/login$'] = "$1/user/login";
Check out the documentation[docs]. There is a very easy way to do this.
$route['(:any)/login'] = '$1/auth/login';
Starx answer is correct but I wouldn't do that, as your first param will be your controller, and if you don't know what's in the dynamic value that's impossible to manage.
I would do :
$route['(:any)/login'] = 'user/login/$1';
This way, 'anything/login' would be redirected to your User controller, to the Login function with the parameter 'anything'.

Resources