I want to write a new controller file, for example:
aaa.php
class aaa extends CI_Controller
{
public function bbb()
{
// Stuff
}
}
how can i enter aaa.php's bbb(),
The example files are begin with welcome.php's index() function.
how can I change that to begin with my new controller file?
If you provide nothing to the base URL, CI will always assume you want the index action. Like localhost/foo will call foo's index() action. With localhost/foo/bar, you will call foo's bar() action. If you want to call localhost and you want to access foo's index(), you need to check that $route['default_controller'] = 'foo'; is correctly setup in your config.php. (If that's not working, check the .htaccess and the index.php to add it manually)
You want to have a separate function run as the Controller's default function? Why not just call this separate function from index()? Beyond this, I'm not really sure what you're asking...the CodeIgniter user_guide is rather extensive if you haven't looked through it.
If you want to use the bbb function of the aaa controller, you just enter this in the url:
www.mysite.com/aaa/bbb/
As Gsto said, to call bbb function enter the url: mysite.com/aaa/bbb
If you want mysite.com/aaa to call bbb() instead of index() by default, your going to want to create the _remap() function in aaa.php controller to call bbb() instead.
See: CI Controllers - Functions Docs
The way to access your controller's methods in CodeIgniter is by uri. The default routing is:
example.com/controller/function/param1/
So to access aaa's bbb() method, you should access the following uri:
/aaa/bbb
If you want to set aaa's bbb() method as the default page of you application, there is two things to do.
You must first tell CodeIgniter to set aaa as your default controller
/* /application/config/routes.php */
$route['default_controller'] = "aaa";
After that, the aaa's index() method will be call by accessing your base site url. You can't tell CodeIgniter to change de default method index() to something else (without setting some routes), so the easiest way to call bbb() by default would be that:
/* /application/controllers/aaa.php */
class aaa extends CI_Controller
{
public function index()
{
$this->bbb();
}
public function bbb()
{
// Stuff
}
}
Related
I simply do not mean how to define a global variable/constant in CodeIgniter.
Let me explain:
I have made a theme engine which is select-able from the current logged in user's control panel. This engine is not very complicated, but a simple folder. anyway, what I do across the application, is that I write one line of code to get the current theme selected by the user. I use a one line of code to get the name, and then store it in a variable:
$theme_name = $this->theme->get_theme_with_slash(false);
And then, I user $theme_name like this to get the proper view:
$this->load->view($theme_name.'result', $data);
And in all of my controllers that loads view I should repeat this process. What I need, is to call the function which gets the theme_name and store in the variable and then use the variable/session/function across the application. My approach currently is the helper's function which is a little less convenient compared to session/variable.
I got this from the manual and this what I have at the top of my config/config.php file: (i have a custom config set to paypal testing)
// HOW TO USE - For example if there's $config['foo'] = 'bar';
// in the config
// using $this- >config->item('foo') will be 'bar'.
// example for my paypal testing:
$config['paypaltest']=0;
http://ellislab.com/codeigniter%20/user-guide/libraries/config.html
and how to access in a controller:
$paypaltest = $this->config->item('paypaltest');
Create A core controller, since your process requires logical operations then you need a method for that.
application/core/MY_Controller.php
class MY_Controller Extends CI_Controller
{
protected $default_theme = 'theme';
public function __construct()
{
parent::__construct();
}
public function get_theme()
{
//your code for selecting the current theme selected from
//the database
$theme_from_db = '';
return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
}
}
Your Controller must extend MY_Controller
application/controller/view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
in code igniter global constants can be defined in
config->constants.php
even you no need to load it,it automatically autoloaded by CI automatically.
In Codeigniter all constant is defined inside application/config/constant.php.
like: define("CONSTANTNAME","value");
Constant degined here is accessible throughout all pages, ie; controllers, models and views
All Codeigniter controllers seem to start with:
public function index()
{
// stuff
}
Is this a requirement or just good practice? I have an instance where an index may not be needed, for example I have a controller called "Auth" and in it there is a function to register and a function to login - you could argue that the login function is of higher priority but in the interest of naming convention I would rather name my functions. What is best practice here?
It's not a must method. It simply behaves like index.html on apache server.
When there is no html file specified, it automatically goes to index.html.
The same here, when there is no controller method specified index is default.
The index method is simply what's called when the second URL segment is missing. For example:
class Auth extends CI_Controller {
public function index () {
// domain.com/auth
// domain.com/auth/index
}
public function register () {
// domain.com/auth/register
}
}
If you don't need that route, you don't need an index method.
i have one default controller in codeigniter
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
as you know we can call this function
sitename/index.php
or
sitename/
as i have url rewrite in .htaccess
now i want to call same index function with parameter passing like
sitename/seo-services
where seo-services will be paramater passing to index function to load seo-services page.
as default syntax of codeigniter is
example.com/class/function/ID
i want in this formet
example.com/class/ID
where i want to skip the function for default (index) function of class
how can i do this?
Thanks
as you can see i am trying this for seo purpose.
Parameters appended to your url are automatically converted to arguments to your method.
For example if you have a method called index index in your welcome controller, then http://www.example.com/index.php/welcome/index/[parametervalue] will automatically pass [parametervalue] to your method, assuming that you define your function to receive the parameter
class Welcome extends CI_Controller {
public function index($parameterValue=null)
{
//do whatever you need here
//note the default value just in case it is null
}
}
If you want to make things shorter, you will have to define an alias in the config/routes.php file. Also, if you want to get rid of the index.php, you will have to configure your web server accordingly (either through .htaccess if using apache or web.config if using iis)
Small mystery: can't seem to pass views between my Index controller (chartAction) and my view. When I go to my localhost it is not accessing the view phtml- instead it is just showing the controller every time (i.e: if I write "echo "HELLO WORLD!""; in my controller I get that echoed...but if I do a $this->view->test = "Hello World!" then access the index.phtml and type in echo $this->test; I get nothing (it still defaults to the controller action). Is there a step that I'm missing here? Why is my $this->view not functioning? I used the command line to create the view so I'm pretty sure that should be set up correctly.
Do I need to register something? Thanks for any help!
Assuming a standard MVC setup of ZF1.x, there is a definite relationship between the url, the controller and the action.
The url http://mydomain.com/index would call the index action of the index controller, typically the index action is the default action and is called automatically. the view script would be /application/views/scripts/index/index.phtml
The url http://mydomain.com/index/chart would call the chart action of the index controller and the view script would be /application/views/scripts/index/chart.phtml
Keep in mind that this behavior is changeable based on configuration and routing options.
It sounds like you may be fairly new at working with ZF. So something like the following may help demonstrate the relationship :
// application/controllers/IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
}
public function indexAction()
{
$this->view->test = "Hello World, from the indexAction().";
}
public function chartAction()
{
$this->view->test = "Hello World, from the chartAction().";
}
// application/views/scripts/index/index.phtml
<?php echo $this->test ?>
// application/views/scripts/index/chart.phtml
<?php echo $this->test ?>
now test your application by calling the url's:
http://yourDomain.com/index/index
http://yourDomain.com/index/chart
If your setup is correct you will see the proper response in your pages.
Case 1: View disabled for just one action:
Look for the following code in your action.
$this->_helper->viewRenderer->setNoRender(true);
Case 2: View disabled for all actions in a specific controller:
Look for the above line in either the init() or preDispatch() functions of the controller.
Case 3: View disabled for all actions in all controllers:
Check case 1 and 2. Also, look for something like the following in your Bootstrap.php:
$frontController->setParam("noViewRenderer", true);
If you find the code like above, you will have to comment it out to get the view working. I am sure there are more possibilities to disable view. Those are to be checked after this.
Your view is disabled..check these lines of code in your action or init of your controller or even the class your controller may be extending
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
UPDATE
You are doing it in your chartAction and echoing in your index.phtml you must do that in your chart.phtml
I am currently using the codeigniter tank_auth, at the start of every controller method I have to do the following:
$data['profile'] = $this->tank_auth->get_profile();
The main reason I do this is to display the current logged in username, and also get their privilege level.
I am going over the code trying to go by the DRY principle and have moved a lot of repeated code over to the _constructor method (Like checking if the user is logged in). I am just wondering if there is a way to move this code from the start of every method to the constructor.
My current constructor method looks like so:
public function __construct()
{
parent::__construct();
// If the user isn't logged in redirect to login page.
if (!$this->tank_auth->is_logged_in())
redirect('auth/login');
}
Thanks!
Add variable $data to the controller and use it for all your view data. For example:
public function __construct()
{
parent::__construct();
$this->data['profile'] = $this->tank_auth->get_profile();
}
When calling the view remember to call it like this:
$this->load->view('my_view', $this->data);
You can also extend CI_Controller with MY_Controller and put the login check in the constructor of MY_Controller. Just extend all controllers which need this check from MY_Controller instead of CI_Controller.