Almost everyone knows about MVC by learning a framework. This may be very familiar with the framework. Once a simple page of the framework cannot be written, let alone designing the MVC Architecture, in fact, there is no such thing.
Almost everyone knows about MVC by learning a framework. This may be very familiar with the framework. Once a simple page of the framework cannot be written, let alone designing the MVC Architecture, in fact, there is no such thing.
Review
In the previous tutorial, we used the codingbean/macaw Composer package to construct two simple routes. The first one is to respond to GET '/fuck', and the other will hold all requests. In fact, for the PHP framework, everything is possible with routing. So what we need to do next is to make the MFFC framework more standardized and plump.
This involves another value of the PHP framework: the establishment of development specifications to facilitate 'multi-person collaboration ', the use of ORM, 'template engine and other tools to 'improve development efficiency '.
Officially start planning folders
Create MFFC/app folders, create three folders: controllers, models, and views in the app, and start the MVC journey.
(Who said I copied Laravel? I copied Rails:-D)
Use namespace
Create the controllers/BaseController. php file:
<? Php
/**
* BaseController
*/
Class BaseController
{
Public function _ construct ()
{
}
}
Create the controllers/HomeController. php file:
<? Php /**
* \ HomeController
*/
Class HomeController extends BaseController
{
Public function home ()
{
Echo "Controller successful! ";
}
}
Add a route entry: Macaw: get ('', 'homecontroller @ home'); 'and open the browser to access it: 81/'. The following prompt is displayed:
Fatal error: Class 'homecontroller' not found in/Library/WebServer/Documents/wwwroot/MFFC/vendor/codingbean/macaw/Macaw. php on line 93
Why can't the HomeController class be found? Because we didn't let him load it automatically, modify composer. json:
{
"Require ":{
"Codingbean/macaw": "dev-master"
},
"Autoload ":{
"Classmap ":[
"App/controllers ",
"App/models"
]
}
}
Run composer dump-autoload. Wait a moment and refresh. You will see the following content (don't forget to adjust the encoding ~) :
Congratulations! namespace used successfully!
Connect to database
Create a new models/Article. php file with the following content ):
<? Php
/**
* Article Model
*/
Class Article
{
Public static function first ()
{
$ Connection = mysql_connect ("localhost", "root", "password ");
If (! $ Connection ){
Die ('could not connect: '. mysql_error ());
} Mysql_set_charset ("UTF8", $ connection); mysql_select_db ("mffc", $ connection); $ result = mysql_query ("SELECT * FROM articles limit 0, 1 "); if ($ row = mysql_fetch_array ($ result )){
Echo ''. $ row [" title "].'';
Echo'
'. $ Row ["content"].'
';
} Mysql_close ($ connection );
}
}
Modify the controllers/HomeController. php file:
<? Php/*** \ HomeController */class HomeController extends BaseController {public function home () {Article: first ();}}
Refresh. At this time, we will get information not found in the Article class, because we did not update the automatic loading Configuration:
Composer dump-autoload
During the waiting time, we will create the database mffc' and create the table articles in it. We will design two fields 'title' and 'content' to record information and fill in at least one piece of data. You can also run the following SQL statement after the mffc database is created:
Drop table if exists 'articles ';
Create table 'articles '(
'Id' int (11) unsigned not null AUTO_INCREMENT,
'Title' varchar (255) default null,
'Content' longtext,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8;
Lock tables 'Article' WRITE;
/*! 40000 alter table 'articles' disable keys */;
Insert into 'articles' ('id', 'title', 'content ')
VALUES
(1) 'I am the title' and' I am the content ~~
I am really a content. Forget it, hum ~ O (partition _ partition) O
'),
(2) 'I am the title' and' I am the content ~~
I am really a content. Forget it, hum ~ O (partition _ partition) O
');
/*! 40000 alter table 'Article' enable keys */;
Unlock tables;
Then, Refresh! You will see the following page:
Congratulations! Both M and C in MVC have been implemented! Next we will call V (view ).
Call View
Modify models/Article. php:
<? Php
/**
* Article Model
*/
Class Article
{
Public static function first ()
{
$ Connection = mysql_connect ("localhost", "root", "C4F075C4 ");
If (! $ Connection ){
Die ('could not connect: '. mysql_error ());
}
Mysql_set_charset ("UTF8", $ connection );
Mysql_select_db ("mffc", $ connection );
$ Result = mysql_query ("SELECT * FROM articles limit 0, 1 ");
If ($ row = mysql_fetch_array ($ result )){
Return $ row;
}
Mysql_close ($ connection );
}
}