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;
Related
I am new to Laravel and working on existing code.
I want to pass some values in url in format of URL/val1/val2/val3.
Every thing is working perfect if all values with normal string or number
but if any value has special character like slash / or \ it shows errors.
eg.
working :- URL/abc/pqr/xys
but if val3 = 22/06 ;url is URL/val1/val2/22/06 error shows 404 not found
If I encoded val3 using javaScript's function encodeURIComponent()
val3=22%2F06 and url become URL/val1/val2/22%2F06 shows Object not found!
// My current route web.php is:-
Route::get('/export/{name}/{status}/{search}', 'ReportController#export')->name('export');
//routes.php
Route::get('view/{slashData?}', 'ExampleController#getData')
->where('slashData', '(.*)');
Your route accept only 3 params. But you pass four params.
Route::get('/export/{name}/{status}/{search}', 'ReportController#export')->name('export');
You must change your val3=22-06. Don't use / as value of your param.
Eg.
URL/val1/val2/22-06
You need to use regex expression for that situation:
Route::get('/export/{name}/{status}/{search}', 'ReportController#export')->name('export')->where(['search' => "[\w\/]+"]);
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.
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.
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
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'.