【问题标题】:I'd like to know about class Route used in web.php我想知道 web.php 中使用的类 Route
【发布时间】:2017-05-12 23:55:08
【问题描述】:

所以,在 Laravel 中有一个 web.php 文件,其中使用了类 Route,并调用了它的静态函数 get 和 match。

问题是,这个类对我来说是一种谜,我在我的 laravel 项目中找不到它的源代码,我在互联网上也找不到任何关于它的信息。 如果你用谷歌搜索,你会找到 Illuminate\Routing\Route 但我认为这不是我要找的类,因为那个类没有获取和匹配的静态函数。 我也尝试在我的项目目录中查找它,我发现我认为有四个具有这样名称的类,但它们都没有在我的 web.php 中使用的这些函数。

这是我的 web.php:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/', 'BlogController@all')->name('post.all');

Route::match(['get', 'post'], '/article/create', 'BlogController@create')->name('post.create')
     ->middleware('auth');

Route::get('/article/{id}', 'BlogController@single')->name('post.single');

Route::match(['get', 'post'], '/article/{id}/delete', 'BlogController@delete')->name('post.delete')
     ->middleware('auth', 'author');

Route::match(['get', 'post'], '/article/{id}/edit', 'BlogController@edit')->name('post.edit')
     ->middleware('auth', 'author');

Route::get('/author/{id}', 'BlogController@author')->name('post.author');

Route::get('/category/{id}', 'BlogController@category')->name('post.category');

Route::match(['get', 'post'], '/user/create', 'UserController@create')->name('user.create')
     ->middleware('auth');

Route::get('/home', 'HomeController@index');

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:

    你快到了;

    您可以在 Illuminate\Routing\Router 类下找到它。

    您在这里看不到静态函数的原因是,Laravel 使用了一种叫做“Facades”的东西,它提供了一种访问实例化类的静态方法。它本质上包装了 Route 类并为您调用这些函数。

    你可以通过查看 aliases 键下的 config/app.php 来查看所有注册到 Laravel 的门面(包括 Route )。

    【讨论】:

      【解决方案2】:

      你正在寻找Illuminate\Routing\Router

      【讨论】:

        【解决方案3】:

        Laravel 使用一些 OOP 概念让您的生活更轻松,但另一方面,正如您发现的那样,它也让您难以“深入了解”并了解实际情况。

        很多这些概念在文档中都有很好的解释,但我认为您正在查看的概念可以在 https://laravel.com/docs/5.3/facades#how-facades-work 找到。我还将向下滚动到 Facade Class Reference 部分,您将能够轻松地查看每个 Facade“指向”哪些类。从本质上讲,这一切都归结为使用魔法方法__callStatic() 让这种魔法发生。

        文档中还有一个“核心概念”的子标题。我建议通读其中的每一个,以熟悉服务容器的工作原理和使用方式,这将使您更好地了解外观本身的工作原理。

        我还将假设您想找到 Route 外观背后的类以查看还有哪些其他方法,在这种情况下,您还应该查看项目 https://github.com/barryvdh/laravel-ide-helper,这将使您的 IDE 更好每个外观提供的所有方法的想法。它可以为您节省大量时间。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-10
          • 1970-01-01
          • 1970-01-01
          • 2021-10-17
          • 2020-09-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多