Laravel在哪里声明名称空间? [英] Where are namespaces declared in Laravel?

查看:49
本文介绍了Laravel在哪里声明名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中了解到,要使用命名空间,您必须先声明或包含.

I learned in php that to be able to use a namespace, you have to declare or include first.

<?php
    namespace namespace_01;

    function f1()
    {
        echo 'this is function';
    }

    use namespace_01 as test_namespace;
    test_namespace\f1();
?>

几乎所有的laravel代码都使用名称空间.但是它们在哪里定义?

Almost all of laravel codes use namespaces. But where are they defined?

示例

当我创建一个控制器时.

when I created a controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class controller1 extends Controller
{
    //
} 

在哪里定义 Illuminate \ Http \ Request ?

推荐答案

vendor/laravel/framework/src/Illuminate/Http/Request.php

您将看到在顶部声明为 namespace Illuminate \ Http; 的名称空间,而类名是 Request

There you will see the namespace declared on top as namespace Illuminate\Http; and the class name is Request

,您可以在 composer.json 文件

"autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },

因此,App文件夹中的所有类都将自动随Composer以及供应商文件加载.您不需要每次都包含文件.

so all the classes inside App folder are auto loaded with composer and also the vendor files. you don't need to include files every time.

这篇关于Laravel在哪里声明名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆