[PHP] framework Tutorial: simple use of the codeigniter framework

Source: Internet
Author: User

Codeigniter is a small but powerful php framework.

You can download the framework from the official website.


The following describes the simple operations in codeigniter.

1. Create and Set Controller

1. Create the blog. php file in the Controller directory:

2. The code in blog. php is as follows:

<?phpclass Blog extends CI_Controller {function index(){echo 'Hello World!';}}?>

3. Enter the address. Note: Do not add a suffix. Browsing page:

The data shown here is actually the data in the index function, because the index method is called by default.

Modify the Code as follows:

<?phpclass Blog extends CI_Controller {function index(){echo 'Hello World!';}function hello(){echo 'Hello WHY!';}}?>

After modifying the access path, you will find that the input or no input/index is the access index method. However, if you enter other values, you will access the content of other functions:


In codeigniter, many settings have been configured, And we can modify them directly.

For example, to modify the default access page, there is a route. php In applications/config:

$route['default_controller'] = "welcome";$route['404_override'] = '';

Change default_controller to blog, and enter index. php In the taskbar. The output is helloworld, that is, the blog. PHP page is accessed:

Ii. Create and set a view

1. Create a file blog_view.php in the Views directory:

2. Return to blog. php In the Controller folder and load the blog_view page with simple echo to realize the true value of its controller:

<?phpclass Blog extends CI_Controller {function index(){$this->load->view('blog_view');}}?>

3. Enter the URL on the page to access the website. The page has changed:

3. data transmission between controller and view

1. modify the code in controller as follows:

<?phpclass Blog extends CI_Controller {function index(){$data['myName']="WHY";$data['head']="'s Blog !";$this->load->view('blog_view',$data);}}?>

2. modify the code in the view as follows. Note that instead of using the data array, you can call data directly:

3. browse the website:


PS: In codeigniter <? PHP echo $ myname?> Equivalent to <? = $ Myname?>


Use PHP to output an array.

1. Add an array with the following changes in the controller:

<?phpclass Blog extends CI_Controller {function index(){$data['myName']="WHY";$data['head']="'s Blog !";$data['todo']=array('eat','sleep','call');$this->load->view('blog_view',$data);}}?>

2. Modify the display array in view as follows:

3. Refresh the page and the page will be displayed normally:

If you are not used to endforeach, you can also use the standard PHP language:

Of course, you can also rewrite the constructor method:

<?phpclass Blog extends CI_Controller {function __construct(){parent::__construct(); }function index(){$data['myName']="WHY";$data['head']="'s Blog !";$data['todo']=array('eat','sleep','call');$this->load->view('blog_view',$data);}}?>

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.