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:
<?phpuse 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):
<?phpreturn [ ' driver ' = ' mysql ', ' host ' = ' localhost ', ' database ' = ' MFFC ' , ' username ' = ' root ', ' password ' = ' password ', ' charset ' = ' utf8 ', ' Collation ' = ' and ' utf8_general_ci ', ' prefix ' and ' = ' ];
Modify models/article.php:
<?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!
Build your own PHP framework with Composer Step-up (iv)