my app: site.com/stas/index/controller
1) Everything should be done inside the "Application" folder
2) In the "Config" folder are all the files corresponding to setup, it's possible to add some with the same format.
$config['tablo1']['sous-tablo1'] = "lambda"
(the array should be "$config")
=> important: if you want some config files to be loaded directly in your app:
in autoload.php file, locate the config part and specify which file should be loaded at application startup.
(it's divided in core classes, helpers, l
Only files needed often should be loaded here. Otherwise we can always specify it once we will need it with :
$this->load->config('analytics'); (for example)
3) Routes
inside the config folder: routes are settings for default URLs:
for example, if we want the default homepage to display "analytics" controller (file analytics.php inside controllers folder)
we modify the line :
$route['default_controller'] = "welcome"; to "analytics".
4) Controllers
Each app needs at least a controller to work.
(Views are where templates are set, models are where business-database access library can be written)
A controller match the URL: filename and class should match.
-> the controller file is written in lowcase: analytics
-> the class inside the file has the same name with a Maj : Analytics
this class should extend the "Controller" class.
Note: if the class has a constructor method (method with the same name as the class), it should always call the parent controller class. (parent::Controller(); )
this class should implement the index function that will call display.
URL matching: url/class/function/parameter
To print directly a result: inside the "index()" function, place an echo or a print.
To redirect it to a view: $this->load->view('name_of_the_view', $data_array);
to transmit variables to the view, it should be set into an array:
$data_array['color']="blue";
$data_array['todo']=array('biloute','machin2','truc3');
arrays and objects can also be transmitted...
5) Views
Views can take the name they want and can be organized into folders
To use variables transmitted by the data array from the controller, place php short tags like
6) working with DB
- make sure database classe is loaded in the autoload settings or directly in the controller's constructor class.
- make sure the database setting file is ok
SELECT to select a table (this will put all the table in an array
data_array['requete'] = $this->db->get('table');
with where:
$this->db->where('field-to-query', 'where-variable')
INSERT
$this->db->insert('comments', $_POST);
to display result in the view file: foreach($requete->result() as $row)
to count number of results: $requete->num_rows();
7) working with URL
check that the url helper is loaded either in the autoload file (settings) or in the controller's constructor $this->load->helper('url');
Display part of the URL
$this->uri->segment->3; get the thrid segment of the uri (variable)
Create a link
inside the view file, we can use a anchor function to create a link.
anchor('analytics/report', 'link name')
this link will link to the function "report" in the controller analytics.
Redirect
redirect('controller_name/controller_function')
8) working with forms
check that the form helper is loaded
inside the View file, we can use functions to generate forms tags:
form_open('analytics/destination')
form_hidden('name', $this->uri->segment('3'))
9) Models
Models are PHP classes that are designed to work with information in your database.
structure of a model class:
class Model_name extends Model {
function Model_name()
{
parent::Model();
}
}
Models can then be used in any controller, but should be loaded in the controller's constructor first.
$this->load->model('indicators_model');
10) Adding your own Library
To add your own library, we can place library classes inside Application/libary
Library file name should start with an uppercase, and the class name should be the same as the file name.
Important: if you want to use any data from a config file or any other file,
rather than using "$this->", you should access it with
$CI =& get_instance();
$variable = $CI->config->item('item-in-my-config-file');
(note it should be inside the constructor or any other method)
11) Unit Tests
check that the unit_test library is loaded in the constructor class
$this->load->library('unit_test');
then do the tests as follows:
echo $this->unit->run($data, 'is_array', 'est-ce un tablo');
Library used to enable TDD : Toast (see http://jensroland.com/projects/test/toast/)
test files should be added into /application/controller/test directory
To run all the tests in a test class, simply point your browser to the class name:
http://www.example.com/test/my_test_class
To run an individual test, point your browser to the test function, WITHOUT the 'test_' prefix:
http://www.example.com/test/my_test_class/some_action
to run all the tests classes in the /test/ folder at once, point your browser to the Toast_all controller:
http://www.example.com/test/Toast_all
Attetion: check that Toast_all is in upper case
Note: toast can be used with libraries and models
and they should be loaded first in the test's controller constructor
for ex: $this->load->model('indicators_model');
then properties or methods of the model class can be used inside tests:
$this->indicators_model->get_last_ten_entries();
1) Everything should be done inside the "Application" folder
2) In the "Config" folder are all the files corresponding to setup, it's possible to add some with the same format.
$config['tablo1']['sous-tablo1'] = "lambda"
(the array should be "$config")
=> important: if you want some config files to be loaded directly in your app:
in autoload.php file, locate the config part and specify which file should be loaded at application startup.
(it's divided in core classes, helpers, l
Only files needed often should be loaded here. Otherwise we can always specify it once we will need it with :
$this->load->config('analytics'); (for example)
3) Routes
inside the config folder: routes are settings for default URLs:
for example, if we want the default homepage to display "analytics" controller (file analytics.php inside controllers folder)
we modify the line :
$route['default_controller'] = "welcome"; to "analytics".
4) Controllers
Each app needs at least a controller to work.
(Views are where templates are set, models are where business-database access library can be written)
A controller match the URL: filename and class should match.
-> the controller file is written in lowcase: analytics
-> the class inside the file has the same name with a Maj : Analytics
this class should extend the "Controller" class.
Note: if the class has a constructor method (method with the same name as the class), it should always call the parent controller class. (parent::Controller(); )
this class should implement the index function that will call display.
URL matching: url/class/function/parameter
To print directly a result: inside the "index()" function, place an echo or a print.
To redirect it to a view: $this->load->view('name_of_the_view', $data_array);
to transmit variables to the view, it should be set into an array:
$data_array['color']="blue";
$data_array['todo']=array('biloute','machin2','truc3');
arrays and objects can also be transmitted...
5) Views
Views can take the name they want and can be organized into folders
To use variables transmitted by the data array from the controller, place php short tags like
6) working with DB
- make sure database classe is loaded in the autoload settings or directly in the controller's constructor class.
- make sure the database setting file is ok
SELECT to select a table (this will put all the table in an array
data_array['requete'] = $this->db->get('table');
with where:
$this->db->where('field-to-query', 'where-variable')
INSERT
$this->db->insert('comments', $_POST);
to display result in the view file: foreach($requete->result() as $row)
to count number of results: $requete->num_rows();
7) working with URL
check that the url helper is loaded either in the autoload file (settings) or in the controller's constructor $this->load->helper('url');
Display part of the URL
$this->uri->segment->3; get the thrid segment of the uri (variable)
Create a link
inside the view file, we can use a anchor function to create a link.
anchor('analytics/report', 'link name')
this link will link to the function "report" in the controller analytics.
Redirect
redirect('controller_name/controller_function')
8) working with forms
check that the form helper is loaded
inside the View file, we can use functions to generate forms tags:
form_open('analytics/destination')
form_hidden('name', $this->uri->segment('3'))
9) Models
Models are PHP classes that are designed to work with information in your database.
structure of a model class:
class Model_name extends Model {
function Model_name()
{
parent::Model();
}
}
Models can then be used in any controller, but should be loaded in the controller's constructor first.
$this->load->model('indicators_model');
10) Adding your own Library
To add your own library, we can place library classes inside Application/libary
Library file name should start with an uppercase, and the class name should be the same as the file name.
Important: if you want to use any data from a config file or any other file,
rather than using "$this->", you should access it with
$CI =& get_instance();
$variable = $CI->config->item('item-in-my-config-file');
(note it should be inside the constructor or any other method)
11) Unit Tests
check that the unit_test library is loaded in the constructor class
$this->load->library('unit_test');
then do the tests as follows:
echo $this->unit->run($data, 'is_array', 'est-ce un tablo');
Library used to enable TDD : Toast (see http://jensroland.com/projects/test/toast/)
test files should be added into /application/controller/test directory
To run all the tests in a test class, simply point your browser to the class name:
http://www.example.com/test/my_test_class
To run an individual test, point your browser to the test function, WITHOUT the 'test_' prefix:
http://www.example.com/test/my_test_class/some_action
to run all the tests classes in the /test/ folder at once, point your browser to the Toast_all controller:
http://www.example.com/test/Toast_all
Attetion: check that Toast_all is in upper case
Note: toast can be used with libraries and models
and they should be loaded first in the test's controller constructor
for ex: $this->load->model('indicators_model');
then properties or methods of the model class can be used inside tests:
$this->indicators_model->get_last_ten_entries();
Commentaires
Enregistrer un commentaire
Tell me what you think