Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MVC to build a thinkPHP in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use MVC to build a thinkPHP in PHP. I hope you will get something after reading this article. Let's discuss it together.

one。 What is MVC?

MVC:M refers to the data model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V.

MVC is a design pattern that forcibly separates the input, processing, and output of the application. Using MVC applications is divided into three core components: model, view, and controller. They each handle their own tasks.

C (State change)-> M (change Notification)-> V (View selection)

Advantages: multiple views can share a model, shorten the development cycle, facilitate maintenance and management, reduce development costs and workload.

two。 Know a MVC framework ThinkPHP

ThinkPHP is a fast and simple object-oriented lightweight PHP development framework.

Advantages:

Automatic creation of an easy-to-use MVC schema directory structure

three。 How to build a thinkPHP

Download the ThinkPHP framework package and refer directly to the ThinkPHP.php file in the directory. I downloaded php1.5.0 at:

Http://pan.baidu.com/s/1nt4ZZ0P . Create a new think project and copy the ThinkPHP package to the think directory.

Define ('THINK_PATH',' ThinkPHP/')

Define ('APP_NAME',' think')

Define ('APP_PATH','.')

Require (THINK_PATH. "/ ThinkPHP.php"

$App = new App (); / / the instantiated operating system automatically creates a development directory

$App- > run ()

Index.php- > IndexAction.class.php

Note: 1.index.php points to IndexAction.php,admin.php and points to AdminAction.class.php.

2.IndexAction must inherit the Action class, this operation module has two operations of Index Show, just like a public toilet, if you want to go to a public toilet, you have to go in first, and then see whether it is a men's room or a women's room. Localhost/think/index.php?a=show, you can enter the show () method, and of course, you can modify the 'DEFAULT_ACTION' = >' show'. of convention.php in common (whose Chinese a stands for controller).

4. ThinkPHP is simple and practical.

(1) how ThinkPHP creates a controller: just create a corresponding controller class format name.class.php in the Lib\ Action directory.

(2) Analysis of ThinkPHP URL routing mode: ThinkPHP adopts a single entry mode, which provides a URL route pattern resolution /.

.... index.php/Index/show==....index.php?a=show

(3) ThinkPHP configuration file:\ ThinkPHP\ Common\ convention.php

/ * Module and operation settings * /

'DEFAULT_MODULE' = >' Index', / / default module name

'DEFAULT_ACTION' = >' index', / / default operation name

/ * incorrect setting * /

'DEBUG_MODE' = > false, / / Debug mode is turned off by default

/ / the convention configuration defines that the variable name will be converted to lowercase if the case is arbitrary.

/ / if you want to override the value of the convention configuration, set it in the project configuration file

/ / all configuration parameters can be changed dynamically before taking effect

five. Database operation of ThinkPHP

Before operating the database, we need to add a database connection to the configuration file.

'DB_TYPE'= >' mysql'

'DB_HOST'= >' localhost'

The initials in 'DB_NAME'= >' think_Zw',//zw should be capitalized.

'DB_USER'= >' root'

'DB_PWD'= >'

'DB_PORT'= > '3306'

'DB_PREFIX'= >' think_'

Suppose we set up a think_ zw table

Next, we create a BlogModel.class.php file under the Lib\ Model directory with the following contents:

Class BlogModel extends Model {

}

After defining the model class, we also need to modify the operation method of the Action class to get the data and display it.

We modify the index operation method that was executed by default to the following code:

Class IndexAction extends Action {

Public function index () {

$Zw = new ZwModel ()

$list = $Zw- > findAll ()

Print_r ($list)

}

}

So far, we have only used the controller and the model, and have not touched the view, so let's add a view template to the above application.

First of all, we modify the index operation method of Action, add template assignment and render template operation.

Then there is the core code:

Index.php

Define ('THINK_PATH','ThinkPHP/')

/ / define project name and path

Define ('APP_NAME','think')

Define ('APP_PATH','.')

/ / load the frame entry file

Require (THINK_PATH. "/ ThinkPHP.php"

/ / instantiate a website application instance

$App=new App ()

$App- > run ()

Create a new IndexAction.class.php under the lib directory

Class IndexAction extends Action {

Public function index () {

$Zw= new ZwModel ()

$list = $Zw- > findAll ()

$this- > assign ('title',' ThinkPHP example')

$this- > assign ('list',$list)

$this- > display ()

}

} class IndexAction extends Action {

Public function index () {

$Zw= new ZwModel ()

$list = $Zw- > findAll ()

$this- > assign ('title',' ThinkPHP example')

$this- > assign ('list',$list)

$this- > display ()

}

}

Create a new Model folder under the lib directory, and then a new ZwModel.class.php file.

Convention.php: connecting to the database

'DB_TYPE'= >' mysql'

'DB_HOST'= >' localhost'

'DB_NAME'= >' think_Zw'

'DB_USER'= >' root'

'DB_PWD'= >'

'DB_PORT'= > '3306'

'DB_PREFIX'= >' think_'

Then create a new index folder in default under Tpl, the same as index.php, and then create a new index.html

{$title}

{$vo.name}-{$vo.details}

Note: there will be a red dot in html, it doesn't matter, it doesn't recognize it.

After reading this article, I believe you have some understanding of "how to use MVC to build a thinkPHP in PHP". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report