Use orm,composerorm_php tutorial to build your own PHP framework with composer

Source: Internet
Author: User
Tags autoload php framework

Use composer to build your own PHP framework using Orm,composerorm


Review

After the first three articles on basic preparation, routing, and design mvc, we've got a PHP microarchitecture with a more complete MVC architecture, but there's something worse than a really usable framework: Database encapsulation, this article will tell you how to integrate an ORM Composer package 。

This is the last article in this series, Next I may let us Open the Composer package for the series of titles to share some experience and sentiment, will be mainly published on 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 of the Composer packages meets the following basic requirements:

Native dependent Composer for management
As simple as possible on a good basis (e.g. our super-simple routing package)
Try to be new and use PHP's new features


When it comes to PHP's new features, there is an off-topic. PHP5.3 introduced a 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, will be released, the main contributor is the famous bird brother @Laruence. In addition, the front of the reporter (that is i) just from the micro-Bo report, bird Brother is enlarging recruit, manufacturing legendary PHP7 on the JIT, My big PHP history wheel is unstoppable haha ha! :-P

Installing Illuminate/database

Add a require entry to Composer.json:

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

Using eloquent

Modify the public/index.php to:

Copy the 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 replacing database password):

Copy the Code code as follows:
<?php

return [

' Driver ' = ' mysql ',

' Host ' = ' localhost ',

' Database ' = ' MFFC ',

' Username ' = ' root ',

' Password ' = ' password ',

' CharSet ' = ' utf8 ',

' Collation ' = ' utf8_general_ci ',

' prefix ' and ' = '

];

Modify models/article.php:

Copy the Code code as follows:
<?php

/**

* Article Model

*/

Class Article extends Illuminate\database\eloquent\model

{

Public $timestamps = false;

}

controllers/homecontroller.php no need to change.

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 Article::first () called in HomeController, more than 10 lines of code were used before, and now nothing is done, inheriting a class.

Eloquent more usage see: Eloquent ORM Chinese documentation

Other well-known 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 the artifact also!
Although the Web language camp is constantly introducing new so-called rapid development Framework, it is still more and more like Rails. Eloquent is almost the most like the 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 ...

Currently build a plump can use PHP framework work is basically completed, and then I will continue to improve the MFFC framework, so that she more rich and useful!

In the future there may be template engine, form verification, send mail, permission system management, asynchronous queue, Video audio processing (chaos:-D) and other content, please pay attention to the not yet born let us Open the Composer package series of articles.

Thank you for your reading and accompanying! Good bye!


PHP Composer Some basic usage issues

Question one: Why must the manager as capsule

Because the name is too long, so rename it, easy to use.

Question two: Vendor under autoload.php What is the role, do not say automatic loading, I want to know how to load

Require '. /vendor/autoload.php ';
The code of this file can't be seen, so I don't know what he's doing.

Question three: What is the function of booteloquent ()

I don't see where I define this function, so I can't judge the effect.

PHP Package Management, composer has a domestic mirror

Some, the domestic full volume mirror has the Laravel Frame project group constructs the pkg.phpcomposer.com/
There is also a proxy: 218.244.132.204/proxy/packagist This agent can not use the composer search and notice method

http://www.bkjia.com/PHPjc/903480.html www.bkjia.com true http://www.bkjia.com/PHPjc/903480.html techarticle use composer to build your own PHP framework using Orm,composerorm review after the first three articles on basic preparation, build routing and design MVC, we've got a structural comparison ...

  • 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.