Following on from the previous chapter to construct a Laravel MVC Frameworks – Routing chapter, this chapter describes how to build the Model layer in the framework.

The cases in the previous article all wrote code in routing closures, which are difficult to maintain and manage if there are too many routing definitions. So we first need to implement a Controller to handle our business logic, which is the C layer in MVC.

Setting up a controller

First, we create our controller file by referring to the Laravel directory schema

mkdir  App/Http/Controllers/IndexController

After the IndexController is created, write a function to handle the homepage logic.

<? php namespace App\Http\Controllers; Class IndexController {public function index() {return 'IndexController'; }}

Change the route map to the controller and modify routers.php

$app['router']->get('/index', 'App\Http\Controllers\IndexController@index');

Finally, change composer.json, configure the auto-load path, and then execute the composer dump-autoload

{
    "name": "17ns/laramvc",
    "authors": [{
        "name": "17ns",
        "email": "[email protected]"
    }],
    "require": {
        "illuminate/routing": "*",
        "illuminate/events": "*"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

Adding Model Components

Once you’ve done that, add the illuminate/database component to make an ORM as powerful as Laravel’s.

Execute the following command to begin importing the component

composer require "illuminate/database":"*" 

After the introduction is completed, we will start to improve the configuration of the database. Here, we will set up a config/database.php file according to Laravel as the configuration file of the database connection. After the establishment, we will start to fill in the MySQL configuration information of the local remote server.

<? php return [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'laramvc', 'username' => 'root', 'password' => 'localdb001', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', ];

After the configuration is complete, you need to launch the Eloquent ORM in your home entry file as follows:

<? php use Illuminate\Container\Container; use Illuminate\Events\EventServiceProvider; use Illuminate\Http\Request; use Illuminate\Routing\RoutingServiceProvider; use Illuminate\Database\Capsule\Manager; // call the auto-load file require __DIR__. '/.. /vendor/autoload.php'; $app = new Container (); $app = new Container (); with(new EventServiceProvider($app))->register(); with(new RoutingServiceProvider($app))->register(); $dbManager = new Manager(); $dbManager = new Manager(); $dbManager->addConnection(require '.. /config/database.php'); $dbManager->bootEloquent(); // load the routing configuration require __DIR__. './.. /app/Http/routes/routers.php'; / / instantiate and distributed processing request $request = request: : createFromGlobals (); $response = $app['router']->dispatch($request); // return response request $response->send();

The database Manager adds the config/database.php connection to the database. After that, we start the Eloquent ORM using the booteLoquent function.

Create a Model

After the above steps are correct, start to create the Model file, here again refer to Laravel, create an App/ user.php

<? php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $timestamp = false; }

After the Model is created, it is introduced into our controller for use

<? php namespace App\Http\Controllers; use App\User; class IndexController { public function index() { dd(User::find(1)); Return 'Home page successfully accessed, here is IndexController'; }}

We look up the users table for the user with id 1, and we print it out using the dd function.