Build your own PHP framework with Composer Step-up (iv)

Source: Internet
Author: User
Tags autoload php framework

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:

    1. Native dependent Composer for management
    2. As simple as possible on a good basis (e.g. our super-simple routing package)
    3. 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)

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.