CodeIgniter View & Model & Controller, codeigniter_PHP Tutorial

WBOY
Release: 2016-07-13 10:22:01
Original
839 people have browsed it

CodeIgniter视图 & 模型 & 控制器,codeigniter

--------------------------------------------------------------------------------------------------------

载入视图

  $this->load->view('home/name');  //可以用子文件夹存储视图,默认视图文件以'.php'结尾

 

载入多个视图

  $data['title'] = 'chenwei';      //给视图添加动态数据

  $data['message'] = 'Your message';

  $this->load->view('header', $data);  //当一次性载入多个视图时,你只需在第一个视图传入数据即可(header视图显示title, content视图显示message)

  $this->load->view('menu');

  $this->load->view('content');

  $this->load->view('footer');

使用对象的例子:

  $data = new Someclass();

  $this->load->view('blogview', $data);

 

视图文件中的变量

  <?php echo $title; ?>

  

 

创建循环

  class Blog extends CI_Controller{

    function index()

    {

      $data['todo_list'] = array('clean house', 'call mom', 'run errands');

      $data['title'] = 'my real title';

      $data['heading'] = 'my real heading';

      

      $this->load->view('blogview', $data);

    }

  }

 

  <?php echo $title; ?>

  

  

        

        

  •     

      

 

获取视图内容(赋值给一变量)

  $buffer = $this->load->view('blogview', $data, true);

  //view函数第三个可选参数可以改变函数的行为。如果将view第三个参数设置为true(布尔),则函数返回数据。view函数缺省行为是 false,将数据发送到浏览器。如果想返回数据,记得将它赋到一个变量中。

 

@黑眼诗人:参考用户手册 PHP替代语法 

视图文件的PHP替代语法 =>

  config/config.php中打开$config['rewrite_short_tags'],那么如果你的服务器不支持短标记,CodeIgniter将重写所有短标记。

  注:如果你使用这个特性,如果在你的视图文件中发生 PHP 错误,则错误信息和行号将无法准确显示。相反,所有的错误将显示为 eval () 的错误。

正常的echo形式:

使用替代语法:

 

替代控制结构

  

        

        

  •     

      

  注:这里没有大括号。相反,结束大括号被替换成了 endforeach 。上面列出的每一个控制结构也有相似的关闭语法:endif, endfor, endforeach 和 endwhile,并且在每个结构以后注意不要使用分号(除了最后一个),用冒号!

  

    

Hi chenwei.

  

    

Hi Joe

  

    

Hi unknow user

  

 

--------------------------------------------------------------------------------

 

模型类文件均存放在 application/models 目录,当然也可以建立子目录,便于大型项目开发管理。

基本的模型类

  1.类名首字母必须大写,其它字母小写,确保继承基本模型类CI_Model,文件名是模型类名的小写形式。

  2.模型可以在控制器中被引用。

    如:$this->load->model('User_model'); 或 $this->load->model('home/User_model');

   模型一旦被载入 就可以使用,默认情况下模型名称直接被引用作为对象名。

    如:$this->User_model->function();

   当然可以重新命名对象名,通过在加载模型函数中指定第二个参数来设定。

    如:$this->load->model('User_model', 'fubar');

      $this->fubar->function();

 

自动载入模型

  如果需要特定模型在整个项目中起作用,可以让CI在初始化时自动装载,通过在application/config/autoload.php文件的自动装载数组中添加该模型。

 

连接到数据库

  模型被载入时不会自动连接数据库,以下方法可以使你连接数据库,

  1.标准方法连接数据库

  2.把第三个参数设置为TRUE来使模型装载函数自动连接数据库

    $this->load->model('User_model', '', TRUE);

  3.手动设定第三个参数来载入你的自定义数据库配置

    $config['hostname'] = 'localhost';

 $config['username'] = 'root';

 $config['password'] = 'root';

 $config['database'] = 'test';

 $config['dbdriver'] = 'mysql';

 $config['dbprefix'] = '';

 $config['pconnect'] = FALSE;

 $config['db_debug'] = TRUE;

 

 $this->load->model('User_model', '', $config);

   //Note: The memory consumption is the same when automatically connecting to the database and manually connecting to the database.

Full example:

class User_model extends CI_Model{

 var $title = '';

 var $connect = '';

var $data = '';

Function __construct()

 {

parent::__construct();

 }

Function get_last_ten_entries()

 {

$query = $this->db->get('entries', 10);

return $query->result();

 }

Function insert_entry()

 {

  $this->title = $this->input->post('title'); //Receive the data submitted by POST, using the input class

  $this->content = $this->input->post('content');

  $this->date = time();

$this->db->insert('entries', $this);

 }

Function update_entry()

  { 

  $this->title = $this->input->post('title');

  $this->content = $this->input->post('content');

  $this->date = time();

  $this->db->update('entries', $this, array('id'=>$this->input->post('id'))));

 }

 }

//The function used above is the Active Record database function

-------------------------------------------------- -------------------------------------------------- ----

Controller files are generally saved in the application/controllers/ folder:

Default URL routing configuration $config['uri_protocol'] = 'AUTO'; //The default is pathinfo mode, optional

Note: The class name must start with a capital letter, and lowercase letters are invalid.

Basic controller classes

class Blog extends CI_Controller{

Public function __construct()

 {

parent::__construct();

    //The constructor cannot return a value, but it can be used to set some default functions. Make sure your controller extends the parent controller class so that it inherits all its methods.

 }

Public function index()

 {

echo 'Hello World!';

 }

Public function comments()

 {

  $this->load->view('comment');

 }

 }

//Use example.com/index.php/blog/comments to access the comments method

Define default controller

 application/config/routes.php $route['default_controller'] = 'Blog';

Put the controller into a subfolder

Create a new directory under the application/controllers directory and put the controller in it. Note: If you want to use a function under a subfolder, make sure the first fragment of the URI is used to describe the folder. application/index.php/home/blog/comments/123

Private method:

private function _test()

 {

  return $variable = 'aaa'; //Even without the modifier private, as long as the method name is prefixed with an underscore (_), it is a private method and cannot be accessed through the URL.

 }

Reserved method name:

The controller class name cannot be index, such as class Index extends CI_Controller{}, because index is the CI default method name and is included in reserved words. Please refer to reserved words for details.

Redefine method calling rules:

_remap();

Processing output:

 _output(); Please refer to the output class for details.

-------------------------------------------------- --------------------------------------------------

Solution to the frameset problem of codeigniter

Brother, first understand the server program and page program clearly. Frame is the page code, it has nothing to do with ci. If you want to control the src of a certain frame, you must first assign a value to the name attribute of the frame, such as
Control his src, link

What is CodeIgniter?

CodeIgniter is a toolkit for people writing web applications in PHP. Its goal is to enable you to develop projects faster than writing code from scratch. To this end, CI provides a rich set of class libraries to meet common task requirements, and provides a simple interface and logical structure to call these libraries. CodeIgniter can minimize the amount of code that needs to be completed, so that you can focus more on project development.
CodeIgniter is free
CodeIgniter is licensed under an Apache/BSD-style open source license, so you can use it if you want. Read the license agreement for more information.
CodeIgniter is lightweight
Really lightweight. Our core system only requires a few very small libraries, as opposed to frameworks that require more resources. Additional libraries are only loaded on request, based on demand, so the core system is very fast and light.
CodeIgniter is fast
Very fast. It would be difficult for you to find a framework that performs better than CodeIgniter.
CodeIgniter uses the M-V-C model
CodeIgniter uses the Model-View-Controllers approach, which can better separate the presentation layer and logic layer. This is very useful for the template designer of the project, it minimizes the amount of program code in the template. We cover this more in MVC's respective pages.
CodeIgniter generates clean URLs
CodeIgniter generates URLs that are very clean and search engine friendly. Different from the standard string query method, CodeIgniter uses a segment-based method:
example.com/news/article/345 Note: The index.php file is included in the URL by default, but it can be changed by changing .htaccess file to change this setting.
CodeIgniter is powerful
CodeIgniter has a full range of class libraries that can complete most commonly required web development tasks, including: reading databases, sending emails, data confirmation, saving sessions, operating on pictures, and Supports XML-RPC data transmission, etc.
CodeIgniter is extensible
This system can be easily extended through custom class libraries and auxiliary functions, or it can also be implemented through extended classes and system hooks.
CodeIgniter does not require a template engine
While CodeIgniter does come with an optional template parser program, you are not required to use templates. The template engine is completely inconsistent with the performance requirements of localized PHP code. To use the template engine, we need to learn its syntax, which is at least a little easier than learning the basics of PHP. Consider the following PHP code: {foreach from=$addressbook item=name}
{/foreach}, so we chose not to use a dedicated template engine.
CodeIgniter is thoroughly documented
Programmers love writing code and hate writing documentation. Of course we do too, but since documentation is as important as the code itself, we're going to get it done. Moreover, our code resources are extremely clean and easy to comment.
CodeIgniter has a friendly user community
You can... the rest of the article>>
in our community forum

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851761.htmlTechArticleCodeIgniter view model controller, codeigniter ------------------ -------------------------------------------------- ------------------------------------- Loading view $this-load-vi...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template