In our daily use of Laravel framework, there are many design concepts and development ideas worth learning, such as how to make the code more “elegant”, componentization, service container and database ORM in the core architecture, which are all worth exploring.
In this series of blog posts, we will take advantage of the componentization features of Laravel to build a mini-Laravel-like MVC framework.
This application focuses on how illuminate/routing is used to implement the framework’s routing module, which allows requests to be forwarded and routed through configuration definitions.
Initialize a new project
Start by creating a project with whatever name you want, but I’m using LaramVC here
mkdir laramvc
Then create a composer.json file
The filling content is as follows:
{" name ":" your project name ", "the authors" : [{" name ":" the author name ", "email" : "the author email"}], "require" : {}}
Executing the Composer Update to generate the vendor directory and auto-load files.
composer update
After the execution, we modeled Laravel under the project and established APP and public directories to store the core business logic of the project and provide external access logic.
mkdir app && mkdir public
2. Add the routing component
Once that’s done, we add the illuminate/routing component, but that in turn relies on the illuminate/events component, so we need to bring both together.
Perform the composer as follows:
composer require "illuminate/events":"*"
composer require "illuminate/routing":"*"
Introduction is finished, we consult laravel based routing definition file Http/routes/app/routers. PHP
<? PHP $app['router']->get('/', function () {echo 'laramvc'; });
After the route is defined, proceed to create the external access intersection file public/index. PHP with reference to laravel
The contents of the index. PHP file are as follows:
<? php use Illuminate\Container\Container; use Illuminate\Events\EventServiceProvider; use Illuminate\Http\Request; use Illuminate\Routing\RoutingServiceProvider; // 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(); // 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();
Once you have written it, you can test it and run it. In this case, you use nginx to configure it.
3. Nginx deployment runs
After completing the above steps, add the NGINX configuration and restart NGINX to deploy successfully.
server { listen 80; // port server_name localhost; / / domain name root/data/WWW/laramvc/public /; // project path location / {try_files $uri $uri/ /index.php? $query_string; if (! -e $request_filename){ rewrite ^/(.*) /index.php last; } index index.html index.htm index.php; include /usr/local/etc/nginx/conf.d/php-fpm; }}
The results are as follows, so you can easily customize one to take advantage of the componentization of Laravel