I am new to code Igniter framework, and I want to send querystring data, with $this->index(), function, how can i do this, I have done editing in config.php file, to accept query strings.
Your question is not clear at all. As an indication on how to work, though:
As it seems you already did, you must set to TRUE the "enable_query_string" config index:
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
As you see, you also have an index for the $_GET array where controllers and models will be placed. In order to build a url (which you must do manually, since the helpers work with uri segments) you can do something like:
index.php?c=mycontroller&m=mymethod&var1=var1
which maps to Mycontroller() controller class and Mymethod() class method, and works the same as for uri segments. In your methods, to retrieve the query string variables after the method, you can:
use the $this->input->get('var1') input method to retrieve the query string part;
use the "regular" $_GET array (which you have enabled by passing TRUE to the config index, as above), $_GET['var1']
just pass the argument to the method (as in uri segments):
function mymethod($var1)
{
echo $var1;
{
"c" and "m" are default triggers, which you can obviously change to whatever you like (just set them in the 2 config indexes, of course).
In Code Igniter you may use $this->input->get() inside your controller functions. You can also use PHP's $_GET array. More information in the documentation at http://codeigniter.com/user_guide/libraries/input.html
Related
I am working on one project in that I am passing users data via query string in url , so I have enabled it inside config.php but after this if I try to call other methods in project then it is not working and again when disabled query string in config.php all methods are working fine, I don't know why enable query string affected on all methods in controller.
Note: I am using routing so it is lik this
$config['enable_query_strings'] = TRUE;// afte making true unable to call other methods in controller.
$route['xyz_method/(:any)'] = 'controller/method1/$1';
$link=urlencode(base64_encode("some_data"));
<li>Click here</li>
"...
CodeIgniter optionally supports this capability, which can be enabled in your application/config.php file. If you open your config file you’ll see these items:
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
If you change “enable_query_strings” to TRUE this feature will become active. Your controllers and functions will then be accessible using the “trigger” words you’ve set to invoke your controllers and methods:
index.php?c=controller&m=method
Note
If you are using query strings you will have to build your own URLs, rather than utilizing the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with segment based URLs.
..."
See documentation here
To work this, try using this
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = TRUE;
My method within the controller can receive this:
domain.com/busqueda/anything
That will search within my database for'anything' and paginate the results, so if more than one then a 3rd value is needed:
domain.com/busqueda/anything/10
The '10' will be the offset.
Using a form with GEt method will result in:
domain.com?busqueda=anything
Which my controller won't accept. So i need to rewrite it to:
domain.com/busqueda/anything/
And be able as well to accept the offset value when typed or linked directly like:
domain.com/busqueda/anything/10
I'm pretty bad when htaccess comes. I have tried some rules but only worked with no 'offset'.
You can make another controller method for form, where you'll get GET parameter and make a redirect: redirect('busqueda/'.$this->input->get('busqueda'));
In case of having problems with routing:
In application/config/routes.php add:
$route['busqueda/(:any)/(:num)'] = 'busqueda_controller/busqueda_method/$1/$2';
$route['busqueda/(:any)'] = ''busqueda_controller/busqueda_method/$1';
In you busqueda_controller/busqueda_method just make:
$offset = false; //no offset
if(FALSE !== $this->uri->segment(3)) {
$offset = (int)$this->uri->segment(3); //I've used casting, because I don't know how CI does it. Maybe it's unnecessary here
}
In CakePHP, it is possible to get the called function string using the
$this->action
syntax. It returns the literal string of whatever is called, so if the URL is /do_this, it returns do_this, and if it's doThis it'll return doThis. Regardless of the called method's real name.
What I am looking for, on the other hand, is the called method's actual name, no matter the URL syntax.
Is there a way to find it out?
I'd preferably be able to do this in the beforeFilter method.
You should use the request object.
CakePHP 3.3 and below
$this->request->params['action'];
Since 3.4
$this->request->getParam('action');
I think this should contain the real method name that was called. CakePHPs router resolves the string URL to a controller / action pair and other args, all of that ends up in the request object. Read the documentation and do debug($this->request); in your beforeFilter() to see what else is there.
In CakePHP 2 you can use $this->action, in CakePHP 3 you must use $this->request->params['action']
The params array (CakePHP >= 3.4) is deprecated The correct way to get the current action within a controller is :
$currentAction = $this->request->getParam('action');
Have you taken a look at this?
Retrieving the name of the current function in php
This obviously will not work in the beforeFilter. You can set a variable:
private $action_name in the Controller and set it from within the methods and use it afterwards, in afterFilter
Zend Framework 1 had a very simple way of parsing URL routes and setting found params in the $_GET superglobal for easy access. Sure, you could use ->getParam($something) inside the controller, but if the param was found in the URL, it was also accessible via $_GET.
Example for url mypage.com/mymodule/mycontroller/myaction/someparam/5:
ZF1
$this->getParam('someparam'); // 5
$_GET['someparam']; // 5
ZF2
$this->getEvent()->getRouteMatch()->getParam('someparam'); // 5
$_GET['someparam'] // undefined index someparam
Obviously, the difference is that ZF2 does NOT put the route params into the $_GET superglobal.
How do I make it put the parsed parameters into the $_GET superglobal, since extending the controller and just defining a constructor that does that is out of the question (because RouteMatch is not an object yet and cannot be called from the controller's constructor)?
Calling $_GET = $this->getEvent()->getRouteMatch()->getParam('someparam'); in every one of my controllers would work, but I don't want that.
In other words, following the example URL from above, I want to be able to do $_GET['someparam'] and still get the value "5" in any component in the application.
Edit: Looks like I wasn't clear enough, so I'll try to clarify some more. I want whatever param I enter in the URL via /key/value formation to be available in $_GET instantly. I don't really have a problem with getting the param, I know how to get it and I extended Zend's controller so I can just call $this->getParams again like in ZF1, and now all controllers extend that one, I just want the params from the URL to automatically be in $_GET as well, so I can access them easily in third party components which use $_GET natively.
Edit 2: Updated as reaction to Samuel Herzog's answer:
I don't really mind invalidating the SRP in this case, because the libraries are built in such a way that they need direct access to $_GET - they do their own filtering and directly depend on this superglobal. They also directly fetch $_FILES and $_POST for processing, it's just the way their code works.
I've made the following method in the abstract controller:
$this->mergeGet(); which basically makes $_GET absorb all the route matched params and everything works as intended, but seeing as the libraries will be required in every controller/action, it might get tedious to call that method every time. If only the controller had an init() method like in ZF1...
In ZF2, Im using this
$getparams = $this->getRequest()->getQuery();
First of all, you shouldn't use $_GET or any other superglobal directly if you're building on an object oriented stack. The SRP is invalidated this way.
If you have no possibility to change the way of your (3rd party?) librarys to change you might want to hook into the MvcEvent, listen to --event-- and then get the RouteMatch, you may fill $_GET with a simple loop.
For a most-performant answer, you should know if the named library will be needed for every action, just for one module, or only in certain controllers/actions.
If the latest is your use-case, you should write a controller plugin instead.
some example code for the first approach:
namespace YourModule;
use Zend\EventManager\EventInterface as Event;
use Zend\Mvc\MvcEvent;
class Module
{
...
public function onBootstrap(Event $ev)
{
$application = $e->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach('route', function(MvcEvent $mvcEvent) {
$params = $mvcEvent->getRouteMatch()->getParams();
foreach ( $params as $name => $value )
{
if ( ! isset($_GET[$name])
{
$_GET[$name] = $value;
}
}
});
}
}
You could use in your controlller:
$paramValue = $this->params()->fromQuery('your_param_here');
Regards
Can I use CodeIgniter's input class to xss clean GET data like this:
$somevar = $this->input->xss_clean($_GET['somevar']);
CodeIgniter's suggest that xss_clean method should be used for the submitted data.
I wonder whether $_GET vars are submitted or just visiting a URL.
So can i use it in that fashion?
Try using:
$this->input->get()
This function is identical to the post function, only it fetches get data:
$this->input->get('somevar', TRUE);
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
The second optional parameter lets you run the data through the XSS filter. It's enabled by setting the second parameter to boolean TRUE;
The GET array is unset by CI on startup because it uses the URI segments instead.
But you can use the xss_clean method on any var you want, just like your example, but you will find $_GET to be empty. The input class is available everywhere by default.