Codeigniter part 1
Want to support HowtoForge? Become a subscriber!
|
Hello everyone. Today I want to learn Codeigniter MVC. Codeigniter is a light and highspeed MVC that created under the Elislab company. That is free of charge. First step download the Codeigniter from official site : www.codeigniter.com Unzip that in the www directory. When you run that at the first time you see the welcome page with some text. In the Codeigniter we have working with 3 directories that is in the application directory. The directoies names is: Controller , Model and View. In the controller we should to write low level code like connecting to database and etc. In the Model we could write validating inputs , text proccessing , connecting to database and etc. And in the View directory with should to write the view code that users can see. For starting writing code , we can use IDE or editor for managing code easily. I prefer to use Notepad++. that's so light and good for me. You can choice that or Netbeans or Aptana or other IDE/Editors. In the Controller the name of class should be name of file. example: if my file name is 'Home' , my class name should be 'Home'. The Codeigniter works with this style: http://host_name/index.php/class/function/input1/input2/.... The class should make extends from CI_Controller. In the older version we was making extends from Controller. For starting make a new file with 'Blog.php' name in the Controller.In that create a new class like below code: <?php class Blog extends CI_Controller { .... } ?> In the class , we have some definition functions.If we make index function , that run at the first of runnig. What I want to say? If I call the Blog class via http://localhost/index.php/blog , it calls the index function if it have! And If I call http://localhost/index.php/blog/home , it don't calls the index function! Try this code: <?php class Blog extends CI_Controller { function index() { echo "Here is root of the class"; } function home() { echo "Here we are at the home function"; } } ?> If you go to http://localhost/index.php/blog or http://localhost/index.php/blog/index , you see "Here is root of the class". But if you go to http://localhost/index.php/blog/home , you see "Here we are at the home function". Attention: if your class haven't any index function , if you call the class with this address : http://localhost/index.php/blog , you will see error! Then make index function at the first step! Now I want to make relation between Controller and View. Create a new file in View directory with blog_view.php name. this name is not important! in the home function in the Blog controller: <?php class Blog extends CI_Controller { function index() { .... } function home() { $this->load->view('blog_view'); } } ?> And in the blog_view.php: <html> <head> <title>Blogs part</title> </head> <body> Welcome to the blogs part. </body> </html> You saw $this->load->view(view file name). By this command we said to the Codeigniter , do show the file that name is in between parenthesis. Or we can send some value to view via the second argument of that.how? <?php ..... function home() { $data['title'] = 'Blogs title'; $data['body'] = 'Here We are ;)'; $this->load->view('blog_view',$data); } ?> And in the blog_view.php: <html> <head> <title><?php echo $title; ?></title> </head> <body> <?php echo $body; ?> </body> </html> The name of variables is not important! Just you should call they in the view! You can send array to view. <?php ... $data['name'] = array('name' => 'Morteza','family' => 'Ipo') ... ?> And in the view try it with print_r: <html> ... ... <body> <?php print_r($name); ?> </body> </html> End of part 1.
|



Recent comments
1 day 39 min ago
1 day 3 hours ago
1 day 15 hours ago
1 day 17 hours ago
1 day 21 hours ago
2 days 4 hours ago
2 days 13 hours ago
2 days 15 hours ago
2 days 23 hours ago
3 days 54 min ago