


The best way to split the routing file (routes.php) in Laravel_php example
前言
Laravel 的路由功能很强大,默认都是定义在 routes.php 文件中,随着项目越来越大,我们需要的定义的路由越来越多,想象一下,如果几百上千个路由都定义在一个文件中,如何去维护?也许还有不同的人都在同一个文件定义路由,这就造成了冲突,因此我们需要分割 routes.php 文件。
下面介绍一种很优雅的方式。
在 app/Providers/RouteServiceProvider.php
的 map
方法中可以如下定义:
public function map(Router $router) { $router->group(['namespace' => $this->namespace], function ($router) { //require app_path('Http/routes.php'); foreach (glob(app_path('Http//Routes') . '/*.php') as $file) { $this->app->make('App\\Http\\Routes\\' . basename($file, '.php'))->map($router); } }); }
文件组织结构图如下:
这样它会遍历 app/Http/Routes/
文件夹下的文件,遍历每个文件路由类的 map
方法,每个文件的结构都类似,
举个例子:
<?php /** * Created by PhpStorm. * User: xl * Date: 2016/7/4 * Time: 18:07 */ namespace App\Http\Routes; use Illuminate\Contracts\Routing\Registrar; class HomeRoutes { public function map(Registrar $router) { $router->group(['domain' => 'www.tanteng.me', 'middleware' => 'web'], function ($router) { $router->auth(); $router->get('/', ['as' => 'home', 'uses' => 'IndexController@index']); $router->get('/blog', ['as' => 'index.blog', 'uses' => 'BlogController@index']); $router->get('/resume', ['as' => 'index.resume', 'uses' => 'IndexController@resume']); $router->get('/post', ['name' => 'post.show', 'uses' => 'ArticleController@show']); $router->get('/contact', ['as' => 'index.contact', 'uses' => 'IndexController@contact']); $router->post('/contact/comment', ['uses' => 'IndexController@postComment']); $router->get('/travel', ['as' => 'index.travel', 'uses' => 'TravelController@index']); $router->get('/travel/latest', ['as' => 'travel.latest', 'uses' => 'TravelController@latest']); $router->get('/travel/{destination}/list', ['as' => 'travel.destination', 'uses' => 'TravelController@travelList']); $router->get('/travel/{slug}', ['uses' => 'TravelController@travelDetail']); $router->get('/sitemap.xml', ['as' => 'index.sitemap', 'uses' => 'IndexController@sitemap']); }); } }
把路由规则都写到每个文件的 map
方法中,这样一来,就实现了很好的 routes.php 文件的分开管理。此外,你也可以简单的分割,直接把 routes.php 中的定义拆散成多个文件,通过 require
的方式引入,但是哪个更好,一目了然。
那么这样路由分开多个文件后岂不是增加调用次数,会不会影响性能?答案是不必担心。通过 Laravel 的命令:
php artisan route:cache
生成路由缓存文件后,路由只会读取缓存文件的路由规则,因此不会影响性能,这样做让开发更高效和规范。
好了,以上就是Laravel路由文件(routes.php)最佳分割方式的全部内容,希望对大家学习Laravel有所帮助。也希望大家多多支持脚本之家。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Huobi to Binance Transfer Guide: Safe and conveniently transfer your crypto assets Many investors use Huobi and Binance at the same time. This article will guide you how to safely transfer crypto assets on Huobi (HTX), such as TRUMP and USDT, to the Binance platform. Binance is popular for its high security, rich currency and trading pairs, and world-leading trading volume. Binance Exchange’s advantages: the world’s number one trading volume, accounting for 50% of the global market; transparent reserve assets are mainly mainstream stablecoins such as Bitcoin, Ethereum and USDT; it has effectively avoided the potential risks of the US SEC and is one of the most stable and reliable exchanges at present. This tutorial will take TRUMP and USDT as examples

How to master Bootstrap customization and component usage includes: 1. Use CSS variables and Sass preprocessor for style customization; 2. Deeply understand and modify component structure and behavior. Through these methods, a unique user interface can be created to improve the responsiveness and user experience of the website.

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...
