How to pass controller name throught query string Codeigniter - php

How to pass when controller name is inside folder.
index.php?c=welcome
I tried this index.php?c=A/welcome which is not working.

The config file has this:
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';
It says: "// experimental not currently in use"
I looked at the Router class that handles this and it looks like it uses the value passed in ?d= to set the directory...you might want to try that.
It would look like
index.php?d=A&c=welcome

Just pass the folder name before controller and append the variables after it. like
index.php / foldername / controllerName / functionName
NOTE:
Use of "index.php" is mostly depends on you as you have used them with others.

Related

Query string creating problems for all methods in codeigniter

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;

Pass variable to default codeigniter controller

I have my codeigniter setup with a default controller. It's accessible as follows:
site.com/index.php
But it's actually:
site.com/project/index
Where index is the default function.
I would like to do the following:
site.com/project7362
But it actually wants:
site.com/project/index/project7362
Where project name is a variable that is pass into the index function. But by default it looks for project-name as a controller. Is there a way to avoid this?
Essentially what I'm hoping to accomplish is to pass a variable directly after the domain name. A user may create a project, and I want that project to be accessible at domain.com/project_id
"Essentially what I'm hoping to accomplish is to pass a variable
directly after the domain name. A user may create a project, and I
want that project to be accessible at domain.com/project_id"
so another way to do this would be to have it like.
domain.com/project/id
this will give you much more flexibility later in your routes for adding different features.
in config/routes:
$route['project/(:any)'] = 'project/view/$1';
in your project controller
function view($id) {
// clean it
$id = htmlspecialchars($id) ;
if ( ! $project = $this->members->returnProjectBy($id) {
$this->showNoResultsFor($id) ; }
else { $this->show($project) ; }
}
OR -- another way to do this would be to put your defined routes first, and then have project be last (because it requires searching on whatever is there)
$route['home'] = 'page/home';
$route['contact'] = 'contact';
// etc etc so you first define your hard coded routes, and then if its not any of those
// you do a search on whatever the value is to find a project
$route['(:any)'] = 'project/view/$1';
so then your link could be
domain.com/id

Sending QueryString in codeIgniter

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

Change Language in Runtime

I'm trying to change the language of one webapp in realtime using codeigniter.
I follow the online documentation, create the folder for language2 with all the traductions, but when I do:
$this->config->set_item('language', 'portuguese');
It don't change the lang, the only way it works is changing the config file ex:
$config['language'] = "english";
But what I need is change in realtime not changing the config of the framework.
Regards,
Pedro
you can use this code. (this example for ion_auth)
$this->config->set_item('language', 'portuguese');
$this->lang->is_loaded = array();
$this->lang->load('ion_auth', 'portuguese');
this code used in constractor .
$this->lang->is_loaded = array();
top line empty laded languages.
Use hooks instead of class constructor, or even extend the Controller to MY_Controller and call in it's constructor.

CodeIgniter - accessing $config variable in view

Pretty often I need to access $config variables in views.
I know I can pass them from controller to load->view().
But it seems excessive to do it explicitly.
Is there some way or trick to access $config variable from CI views without
disturbing controllers with spare code?
$this->config->item() works fine.
For example, if the config file contains $config['foo'] = 'bar'; then $this->config->item('foo') == 'bar'
Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.
You can do something like that:
$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');
$this->config->item('config_var') did not work for my case.
I could only use the config_item('config_var'); to echo variables in the view
Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.
This is how I did it. In config.php
$config['HTML_TITLE'] = "SO TITLE test";
In applications/view/header.php (assuming html code)
<title><?=$this->config->item("HTML_TITLE");?> </title>
Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];
echo $this->config->config['ur config file']
If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable
$config['50001'] = "your message"
Now I want access in my controller or model .
Try following two cases one should work
case1:
$msg = $this->config->item('ur config file');
echo $msg['50001']; //out put: "your message";
case2:
$msg = $this->config->item('50001');
echo $msg; //out put: "your message"
$config['cricket'] = 'bat'; in config.php file
$this->config->item('cricket') use this in view
If you are trying to accessing config variable into controller than use
$this->config->item('{variable name which you define into config}');
If you are trying to accessing the config variable into outside the controller(helper/hooks) then use
$mms = get_instance();
$mms->config->item('{variable which you define into config}');
Example, if you have:
$config['base_url'] = 'www.example.com'
set in your config.php then
echo base_url();
This works very well almost at every place.
/* Edit */
This might work for the latest versions of codeigniter (4 and above).

Categories