Where are namespaces declared in Laravel?

为君一笑 提交于 2020-12-13 03:39:20

问题


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();
?>

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

Example,

when I created a controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class controller1 extends Controller
{
    //
} 

where is Illuminate\Http\Request defined?


回答1:


Open file from vendor/laravel/framework/src/Illuminate/Http/Request.php

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

and you can see in your composer.json file

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

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.




回答2:


These are found in the Laravel Framework. Laravel uses composer to automatically load these packages. You can find the source in your /vendor folder, this is where composer puts the packages.



来源:https://stackoverflow.com/questions/52754867/where-are-namespaces-declared-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!