Home Backend Development PHP Tutorial The best way to split the routing file (routes.php) in Laravel_php example

The best way to split the routing file (routes.php) in Laravel_php example

Aug 17, 2016 pm 01:02 PM
laravel Tutorial routing

前言

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);
    }
  });
}
Copy after login

文件组织结构图如下:

这样它会遍历 app/Http/Routes/ 文件夹下的文件,遍历每个文件路由类的 map 方法,每个文件的结构都类似,

举个例子:

<&#63;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']);
    });
  }
}
Copy after login

把路由规则都写到每个文件的 map 方法中,这样一来,就实现了很好的 routes.php 文件的分开管理。此外,你也可以简单的分割,直接把 routes.php 中的定义拆散成多个文件,通过 require 的方式引入,但是哪个更好,一目了然。

那么这样路由分开多个文件后岂不是增加调用次数,会不会影响性能?答案是不必担心。通过 Laravel 的命令:

php artisan route:cache
Copy after login

生成路由缓存文件后,路由只会读取缓存文件的路由规则,因此不会影响性能,这样做让开发更高效和规范。

好了,以上就是Laravel路由文件(routes.php)最佳分割方式的全部内容,希望对大家学习Laravel有所帮助。也希望大家多多支持脚本之家。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

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 is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

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

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

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

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

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

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

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...

How to transfer coins to Binance? How to mention the crypto assets in HTX to the Binance platform? How to transfer coins to Binance? How to mention the crypto assets in HTX to the Binance platform? Mar 03, 2025 pm 08:51 PM

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

Advanced Bootstrap Tutorial: Mastering Customization & Components Advanced Bootstrap Tutorial: Mastering Customization & Components Apr 04, 2025 am 12:04 AM

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.

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

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,...

See all articles