A mvc_php example of designing your own PHP framework with composer

Source: Internet
Author: User
Tags autoload php and php framework

Review

In the previous tutorial, we built two simple routes using the CODINGBEAN/MACAW Composer package, the first one in response to get '/fuck ' and the other one that would hold all requests. In fact, for the PHP framework, there is a route for everything. So the next thing we're going to do is make the MFFC framework more standardized and fuller.

This involves the additional value of the PHP framework: setting development specifications to facilitate ' multiplayer collaboration ', using ORM ', ' template engines ' and other tools to ' improve development efficiency '.

Formally start planning folders

New Mffc/app folder, in the app to create controllers, models, views three folders, began to officially embark on the journey of MVC.

(who said I copied the laravel, I copied is clearly Rails:-D)

Using namespaces

New controllers/basecontroller.php File:

<?php
/**
* Basecontroller
*/
Class Basecontroller
{

Public Function __construct ()
{
}
}

New controllers/homecontroller.php File:

<?php
/**
* \homecontroller
*/
Class HomeController extends Basecontroller
{

Public Function Home ()
{
echo }
}

Add a route: Macaw::get (', ' homecontroller@home '); ', open browser direct access to http://127.0.0.1:81/', and the following prompts appear:

Fatal error:class ' HomeController ' not found in/library/webserver/documents/wwwroot/mffc/vendor/codingbean/macaw/ Macaw.php on line 93

Why didn't you find the HomeController class? Because we didn't let him load automatically, modify Composer.json for:

{
"Require": {
"Codingbean/macaw": "Dev-master"
},
"AutoLoad": {
"Classmap": [
"App/controllers",
"App/models"
]
}
}

Run composer Dump-autoload ', wait a moment, refresh, you will see the following content (don't forget to adjust the code OH ~):


Congratulations, the use of namespaces is successful!

Connecting to a database

Create a new models/article.php file with the contents (database password please change yourself):

<?php
/**
* Article Model
*/
Class Article
{
public static function A ()
{
$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 ' Echo ' <p> '. $row [content]. ' </p> ';
}
Mysql_close ($connection);
}
}

To modify the controllers/homecontroller.php file:

<?php/*** \homecontroller*/class HomeController extends basecontroller{public  function Home () {  Article:: A (); }}

Refresh, you will get information that is not found by the Article class because we did not update the auto load configuration:

Composer Dump-autoload

In the waiting time, we go to build the database MFFC ', create the table in the inside articles ', design two fields title ', ' content to record information, and fill in at least one data. You can also run the following SQL statement after establishing the completion MFFC database:

DROP TABLE IF EXISTS ' articles ';
CREATE TABLE ' articles ' (
' id ' int (one) unsigned not NULL auto_increment,
' title ' varchar (255) DEFAULT NULL,
' Content ' Longtext,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8;
LOCK TABLES ' articles ' WRITE;
/*!40000 ALTER TABLE ' articles ' DISABLE KEYS * * *;
INSERT into ' articles ' (' id ', ' title ', ' content ')
VALUES
(1, ' I'm the title ', ' (2, ' I'm the title ', ' /*!40000 ALTER TABLE ' articles ' ENABLE KEYS * * *;
UNLOCK TABLES;

Then, refresh! You will see the following pages:


Congratulations to you! The M and C in MVC are already implemented! Next we start calling V (view).

Call View

Modify models/article.php to:

<?php
/**
* Article Model
*/
Class Article
{
public static function A ()
{
$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);
}
}

Returns the array that contains the results of the query. Modify HomeController:

<?php
/**
* \homecontroller
*/
Class HomeController extends Basecontroller
{
Public Function Home ()
{
$article = Article::first ();
Require DirName (__file__). ' /.. /views/home.php ';
}
}

Save, refresh, you will get the page that looks exactly like the above, the view call succeeds!

Almost everyone learns about MVC by learning a framework, this may be the framework used very familiar, once away from the framework of a simple page can not write, not to mention their own design MVC architecture, in fact, there is not so many doorways, the principle is very clear, I say my sentiment:

1. PHP Framework is no more, he is also PHP, but also to follow the operating principles of PHP and basic philosophy. By grasping this we can easily understand a lot of things.

2. PHP Web site Logically, and PHP test.php no difference, are just a string as parameters passed to the PHP interpreter. It's just that a complex web site calls the files and code that needs to be run based on the URL, and then returns the corresponding results.

3. Whether we see a "small frame" consisting of CodeIgniter such as 180 files, or a "big frame" of laravel such ' plus vendor of more than 3,700 files ', they will assemble a string that can be run at each URL, and pass to The PHP interpreter, and then passes the string returned from the PHP interpreter to the visitor's browser.

4. MVC is a logical architecture, essentially to allow the brain's ultra-low ram computer to produce large software that is far beyond the human brain, but the MVC architecture has been formed before GUI Software appeared, and command line output is also a view.

5. In MFFC, a URL-driven framework does something basically like this: the Portal file require controller, controller require model, model and database interaction to get data back to the controller, controller require view, fill in the data into the view, return to the visitor, process End.

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.