A orm_php instance of using composer to build your own PHP framework

Source: Internet
Author: User
Tags autoload php framework

Review

After the first three articles on the basics of preparation, routing, and design mvc, we've got a PHP micro framework with a fairly structured MVC architecture, but it's one thing worse than a really hands-on framework: Database encapsulation, this article will talk about how to integrate an ORM Composer package 。

This is the last article in this series, Next I may let us Open and open the Composer package for the series title to share some experience and insights, will be mainly published in this site.

Body

We chose Laravel's illuminate/database as our ORM package. I tried a few well-known ORM, found or laravel eloquent easy to use! Let us happy ORM, open and open! :-D

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

Native dependent Composer for management
Try to be as simple as possible on a usable basis (like our very simple routing packet)
Try to be new and use PHP's new features


When it comes to PHP's new features, there's a digression. PHP5.3 introduced the namespace, which is the function of planning in PHP6, so PHP5.3 to a certain extent is PHP6, the next version of PHP is PHP7, is about to release, the main contributor is the famous bird brother @Laruence. In addition to the front of the reporter (i) just from micro-blog report, Bird Brother is amplification recruit, manufacturing legend PHP7 on JIT, My big PHP history wheel is unstoppable ha ha! :-P

Install Illuminate/database

Add a require item to the Composer.json:

"Illuminate/database": "*"
Run composer update and wait for the installation to complete.

Using eloquent

Modify public/index.php to:

Copy Code code 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 ();

Routing configuration

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

New config/database.php (note Replace database password):

Copy Code code as follows:

<?php

return [

' Driver ' => ' MySQL ',

' Host ' => ' localhost ',

' Database ' => ' MFFC ',

' username ' => ' root ',

' Password ' => ' password ',

' CharSet ' => ' UTF8 ',

' Collation ' => ' utf8_general_ci ',

' Prefix ' => '

];

Modify models/article.php:

Copy Code code as follows:

<?php

/**

* Article Model

*/

Class Article extends Illuminate\database\eloquent\model

{

Public $timestamps = false;

}

controllers/homecontroller.php need not be changed.

Refresh, the page is still:

Congratulations to you! Eloquent use success!

Eloquent more usage

Eloquent is exceptionally powerful, and can be said to be the most special and valuable part of Laravel.
Like the call in HomeController Article::first (), preceded by more than 10 lines of code, now nothing to do, inherit a class on the line.

Eloquent more usage see: Eloquent ORM Chinese document

Other famous ORM

There are also many well-known ORM and datamapping (Database migration, etc.) packages, see: ORM and Datamapping

ORM can greatly improve the development efficiency, eloquent is really an artifact also!
Although the Web language camp is constantly introducing a new so-called rapid development framework, it is still more and more like Rails. Eloquent is almost the most like a rails ORM on the surface, but unlike rails, Ruby's super object-oriented features are not covered. Laravel author Taylor Otwell said in an interview that eloquent was the hardest part of the whole laravel. I also found in the debugging, mffc/vendor/illuminate/database/illuminate/database/eloquent/model.php this file has more than 3,000 lines ...

At present, the construction of a full ready to use the PHP framework of the work is basically completed, and then I will continue to improve the framework of MFFC, let her more rich, easy!

There may be template engine, form verification, send mail, permission system management, asynchronous queues, video audio processing (chaos:-D), and so on, please pay attention to have not yet been born let us Open the Composer package series of articles.

Thank you for your reading and company! Good bye!

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.