How to get a url parameter in Magento controller? - magento

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

Related

How can I change name of dynamically generated URL in Codeigniter

I'm having dynamic url for my each search result in Codeigniter, but I want to know how can I change my url. For example right now I'm having the url something like this:
www.xyz.com/vendor/vendor_details?iuL80rpoEMxCi89uK6rIyTgqCGuagQ+BUoUnvyBdx09EawMiFfnaB+q3Q8YyBSFwbOVw8+32ZInJrjE2I42teA==
but I want it like this:
www.xyz.com/Delhi/Balaji-Courier-And-Cargo-Bharat-Singh-Market-Opposite-B-7-Petrol-Pump-Vasant-Kunj/011P1238505881A9D9W7_BZDET?xid=RGVsaGkgSW50ZXJuYXRpb25hbCBDb3VyaWVyIFNlcnZpY2VzIEhhbWlsdG9uIFJvYWQ=
here the example
$route['vendor/(:any)/(:any)/(:any)'] = 'vendor/vendor_details/$3';
See this Documentation here: routing
You want to add
www.example.com/city/address/related_id
This Example can help you to get that.
Here are a few routing examples:
$route['journals'] = 'blogs';
A URL containing the word “journals” in the first segment will be remapped to the “blogs” class.
$route[blog/Joe'] = 'blogs/users/34';
A URL containing the segments blog/Joe will be remapped to the “blogs” class and the “users” method. The ID will be set to “34”.
$route['product/(:any)'] = 'catalog/product_lookup';
A URL with “product” as the first segment and anything in the second will be remapped to the “catalog” class and the “product_lookup” method.
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
A URL with “product” as the first segment and a number in the second will be remapped to the “catalog” class and the “product_lookup_by_id” method passing in the match as a variable to the method.

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.

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