Use ORM to build your own PHP framework with Composer

Source: Internet
Author: User
After the basic preparation, routing, and MVC design of the first three articles, we have obtained a PHP microframework with a complete MVC architecture, however, there is still one difference between a framework that can be used easily: database encapsulation. This article describes how to integrate Review

After the basic preparation, routing, and MVC design of the first three articles, we have obtained a PHP microframework with a complete MVC architecture, however, it is far from a framework that can be used easily: database encapsulation. This article describes how to integrate an ORM Composer package.

This is the last article in this series. Next, I may share some experiences and insights with the Composer package, which we opened again. it will be published on this site.

Body

We chose Laravel's illuminate/database as our ORM package. I tried several well-known ORM and found that Laravel's Eloquent is easy to use! An ORM that makes us happy! :-D

In this series of tutorials, each Composer package must meet the following basic requirements:

Native relies on Composer for management
Make it as simple as possible (for example, our ultra-simple routing package)
Try to be new and use new PHP features


Speaking of the new features of PHP, I have a question. PHP5.3 introduces the namespace, which is a function planned for PHP6. Therefore, PHP5.3 is actually PHP6 to a certain extent. The next PHP version is PHP7, which will be released soon, the main contributor is Laruence @ Laruence. In addition, this front-end Reporter (that is, I) just sent a report from Weibo. laruence is amplifying the recruitment. in The Legend of PHP7 on JIT, the history wheel of my big PHP cannot be blocked. hahaha! :-P

Install illuminate/database

Add a require item to composer. json:

"Illuminate/database ":"*"
Run composer update and wait until the installation is complete.

Use Eloquent

Modify public/index. php:

The code is as follows:


<? Php

Use Illuminate \ Database \ Capsule \ Manager as Capsule;

// Autoload automatic loading

Require '../vendor/autoload. php ';

// Eloquent ORM

$ Capsule = new Capsule;

$ Capsule-> addConnection (require '../config/database. php ');

$ Capsule-> bootEloquent ();

// Route configuration

Require '../config/routes. php ';

Add config/database. php (replace the database password ):

The code is as follows:


<? Php

Return [

'Driver '=> 'mysql ',

'Host' => 'localhost ',

'Database' => 'mffc ',

'Username' => 'root ',

'Password' => 'password ',

'Charset' => 'utf8 ',

'Colation' => 'utf8 _ general_ci ',

'Prefix' =>''

];

Modify models/Article. php:

The code is as follows:


<? Php

/**

* Article Model

*/

Class Article extends Illuminate \ Database \ Eloquent \ Model

{

Public $ timestamps = false;

}

Controllers/HomeController. php does not need to be changed.

Refresh, the page is still:

Congratulations! Eloquent used successfully!

More use of Eloquent

Eloquent is exceptionally powerful and is the most special and valuable part of Laravel.
Like the Article: first () called in HomeController, more than a dozen lines of code have been used before. now you don't need to do anything. just inherit a class.

For more information about Eloquent, see the Chinese document of Eloquent ORM.

Other famous ORM

There are also many well-known ORM and Datamapping (database migration) packages. For more information, see ORM and Datamapping.

ORM can greatly improve the development efficiency, so does Eloquent!
Although the various language camps of the web session are constantly introducing new so-called rapid development frameworks, they will eventually become more and more like Rails. Eloquent is almost the most like a Rails ORM on the surface, but it is incomparable with Rails. Ruby's powerful object-oriented feature is not a cover. Laravel author Taylor Otwell said in an interview that Eloquent is the most difficult part of Laravel. During debugging, I also found that the MFFC/vendor/illuminate/database/Illuminate/Database/Eloquent/Model. php file contains more than 3000 lines ......

At present, the work of building a plump PHP framework that can be used to get started is basically completed. Next, I will continue to improve the MFFC framework to make it richer and easier to use!

In the future, there may be template engines, form verification, sending emails, permission system management, asynchronous queues, video and audio processing (random input:-D), etc, please pay attention to the series of articles about the Composer package that have not yet been released.

Thank you for your reading and companionship! Goodbye!

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.