Introduction to the CI framework by June: an example we all know-"HelloWorld !"

Source: Internet
Author: User
Tags php foreach
Hello, Everyone! I haven't seen you in some days. Fortunately, the end-of-the-world predictions of the Mayans are not true. you can also let the big guys continue to listen to their military elders ~~~ Hello, Everyone! I haven't seen you in some days. Fortunately, the end-of-the-world predictions of the Mayans are not true. you can also let the big guys continue to listen to their military elders ~~~
Today we are going to learn the controllers, methods, and views in the CI framework. then we will complete an example that everyone understands-"Hello World !".


[Content of this lecture]
1. Controllers and methods;
What is a controller? How can I create a simple controller and method and what requirements should I pay attention to when writing a controller and method?


II. View;
How to create and load a view? How to add dynamic data (including variables, arrays, and arrays) to a view )?


3. complete "Hello World !" Example.
Based on the above learning, we are now learning and using controllers and views to complete a simple example.


[Details]
1. Controller and method.

(1) What is a controller?
We all know that the controller refers to the model-view-controller, that is, the C layer in the M-V-C mode, it is mainly responsible for accepting user input and calling models and views to fulfill user requirements without outputting or processing anything. To put it bluntly, the controller is a class file, but its file name is associated with your URL. This should be well understood, as long as you still remember an analysis on how to process the URL of the CI in the previous lecture.


Suppose there is a URL:


Ci_demo/index. php/jayjun/

In the above example, CodeIgniter will try to find and load a controller named jayjun. php.



(2) create a simple controller and method.
Create a new file named jayjun. php in the application/controllers/directory and enter the following code:
Copy code
  1. Class Jayjun extends CI_Controller {
  2. Public function _ construct ()
  3. {
  4. Parent: :__ construct ();
  5. }
  6. }
  7. ?>
Then we write a method in the controller and name it index. As follows:
Copy code
  1. Class Jayjun extends CI_Controller {
  2. Public function _ construct ()
  3. {
  4. Parent: :__ construct ();
  5. }
  6. Public function index ()
  7. {
  8. Echo "This is a test! ";
  9. }
  10. }
  11. ?>


Enter http: // ci_demo/index. php/jayjun/index in the browser. if you are correct, you should see the page output This is a test !.


Pay attention to the following points:

A. the class name must start with an uppercase letter. The following are valid:
Copy code
  1. Class Jayjun extends CI_Controller {
  2. }
  3. ?>

B. make sure that your controller is extended from the parent controller class (named CI_Controller) so that it can inherit all its methods.


C. The URL will load the index () method by default in the controller.
In the preceding example, the index () method exists. you can also access it by entering http: // ci_demo/index. php/jayjun in the browser.


D. Reserved words.
To facilitate programming, CodeIgniter uses a series of functions and names to complete the operation. Therefore, some names cannot be used by programmers. Below is a list of reserved words that cannot be used by programmers.
Because your controller class will inherit the main program controller, your function name must not be the same as the function name in the main program controller class, otherwise your local function will overwrite them. The reserved names are listed below. do not name your controller as follows:
Controller
CI_Base
_ Ci_initialize
Default
Index
In addition, do not name your methods as follows:


Is_really_writable ()
Load_class ()
Get_config ()
Config_item ()
Show_error ()
Show_404 ()
Log_message ()
_ Exception_handler ()
Get_instance ()

Of course, there are still some constants and variables that cannot be renamed. if you want to learn more, you can check the official website's introduction to reserved words.


Just now we introduced the controller C layer in the M-V-C, next we will introduce the View V layer.



2. View.
In M-V-C mode, a view is the interface that the user sees and interacts. To put it bluntly, a view is a webpage or part of a webpage, such as the header, bottom, and sidebar.
(1) create a view first.
Create a file named hello. php in the application/views/directory. The code is as follows:
Copy code
  1. Create a simple view
  2. Hello, everyone. I'm an Army brother! Welcome to PHPer.
  3. Question: What is the nickname of June?

  4. Answer: pork ribs. The source is 'Male and female braised pork slice, female and male braised pork spareri '. Haha ~~


(2) load the view.
Since the controller we write inherits the parent controller, we can call the following function in the method to load a view.


$ This-> load-> view ("name of the view file to be loaded ");
Note that if your view file extension is. php, you do not need to specify the extension in the file for loading the view. In addition to your own extension names (such as .html and. tpl ).


For example, the hello. php file we just wrote can be loaded as follows:

$ This-> load-> view ("hello ");


(3) add dynamic data to the view.
The dynamic data here can be variables, arrays, multi-dimensional arrays, or objects. Okay. let's try it in the controller:
Copy code
  1. Class Jayjun extends CI_Controller {
  2. Function index ()
  3. {
  4. // Here is an example of using variables
  5. $ Data ['title'] = "creating a simple view ";
  6. // Here is an example of using arrays
  7. $ Data ['content'] = array (
  8. "Name" => "Jun Ge ",
  9. "Welcome" => "welcome to PHPer. ",
  10. "Question" => "What is the nickname of June? ",
  11. "Answer" => "pork ribs brother. The source is 'Male and female braised pork slice, female and male braised pork spareri '. Haha ~~ ",
  12. );
  13. // Here is an example of using an object. at this time, the class variable will be converted to an array element. because the following class is a class written randomly, it is only shown here to avoid errors, just commented out.
  14. // $ Data ['other _ content'] = new SomeClass ();
  15. $ This-> load-> view ('hello', $ data );
  16. }
  17. }
  18. ?>
Now open the new hello. php file and replace the text with the dynamic data written in the controller. As follows:
Copy code
  1. <? Php echo $ title;?>
  2. Hello everyone, I am !
  3. Let's take a look at one question:

  4. Answer:



As June said, dynamic data is not limited to simple variables, one-dimensional arrays, but also multidimensional arrays. This is a common situation. for example, retrieving data from a database is a typical multi-dimensional data.


In the preceding example, we add some personal information about the military elder brother, as follows:
Copy code
  1. Class Jayjun extends CI_Controller {
  2. Function index ()
  3. {
  4. // Here is an example of using variables
  5. $ Data ['title'] = "creating a simple view ";
  6. // Here is an example of using arrays
  7. $ Data ['content'] = array (
  8. "Jayjun" => array (
  9. "Jun Ge ",
  10. "26 years old ",
  11. "Anhui ",
  12. "Brothers are single now ",
  13. ),
  14. "Welcome" => "welcome to PHPer. ",
  15. "Question" => "What is the nickname of June? ",
  16. "Answer" => "pork ribs brother. The source is 'Male and female braised pork slice, female and male braised pork spareri '. Haha ~~ ",
  17. );
  18. // Here is an example of using an object. at this time, the class variable will be converted to an array element. because the following class is a class written randomly, it is only shown here to avoid errors, just commented out.
  19. // $ Data ['other _ content'] = new SomeClass ();
  20. $ This-> load-> view ('hello', $ data );
  21. }
  22. }
  23. ?>

Then our view file also needs to be changed:
Copy code
  1. <? Php echo $ title;?>
  2. Hello everyone, I am
  3. ,
  4. Let's take a look at one question:

  5. Answer:



The above figure shows the effect:


3. complete "Hello World !".
This simple example doesn't need to be written by June ~~~


Well, today we have learned the controllers and views in the CI framework and provided some small examples. However, we have not talked about models. This is what we will talk about next.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.