This article brings you knowledge about Laravel directory structure and configuration. Laravel's directory structure is relatively richer in the initial state. In addition to the traditional controller, it also helps us prepare The directory of code files such as scripts and middleware can basically be used directly. I hope it will be helpful to everyone.
Laravel’s directory structure will be relatively richer in the initial state. In addition to the traditional controller, It also helps us prepare directories for code files such as scripts and middleware, which can basically be used directly.
First let’s take a look at what’s in the root directory.
In fact, you can know the functions of these directories based on their names. For example, the app directory is the specific application code. The config directory stores configuration file information. In the previous article, we mentioned that if you use Laravel in a virtual machine, you need to use the file server.php in the root directory. In fact, this file loads public/index.php in the root directory. document.
bootstrap is a file that needs to be loaded when starting the framework. Generally, the contents of this file are not modified. This directory also contains cache-related directory files. database is obviously database related content. public is the entry directory of our framework. Other resource files can also be placed here, such as directly displayed images and static files. resources stores views and uncompiled resource files.
routes directory is the directory where routing files are stored. This directory is very important. Of course, it is actually the routing files inside that are very important. It contains web.php, api.php, channels.php and console.php by default, which respectively represent the default web request routing, api request routing, registration event broadcast and closure-based console script commands.
The storage directory is used to store various files generated by the application, including cache, logs and other information. The tests directory contains content related to automated testing.
In these directories, let’s focus on the content contained in the app directory.
The app directory is the most commonly used directory in our application development. The controllers, models, middleware and other content of our application are all in this directory.
The Console directory is the command line script directory we wrote, that is, the command line functions that can be customized and run through php artisan are all in this directory.
Exceptions are exception classes that we can customize. Models store our customized data models. The Providers directory stores default and some service providers that we can customize.
Next is the Http directory.
Controllers Needless to say, the controllers are all written here. Middleware contains the default middleware. Of course, our customized middleware can also be written in this directory.
Kernel.php is the control file for requesting the kernel. In this file, we can define the requested middleware. This is also a very important core document. We will explain it in detail when we learn it in the future.
The content about the directory structure is actually the above. Next, let’s take a look at the configuration file, which is the content in the config directory under the root directory. The content here is also what we often Need to be exposed.
In fact, you can see their functions from the names of these configuration files. In the next article, we will soon come into contact with the database.php file, because in the entry-related content, we still need to simply connect to the database to experience it.
In database.php, you can not only define the mysql database information to be connected, but also the NoSQL type database to be connected (redis connection configuration has been given by default). Let’s take a look at the MySQL connection information here.
'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
Through the configuration file code, we can see that a lot of information is obtained through the env() function. The content obtained by this function is actually the content of the .env file in the root directory. Opening this .env file, we can see that the configuration method is similar to that of the php.ini file, and both have configuration information in the form of key=value.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=
Here, we can configure the database connection information in the current environment. What are the benefits of this configuration?
独立的配置文件这种形式的很容易实现配置中心,也很容易实现测试环境和正式环境的分别部署。一般我们不会将这个 .env 放到 git 中,或者跟随代码上传。在正式环境或者测试环境都是手动地或者通过配置中心来进行配置。这样的话,我们就不需要修改源代码,只需要使用不同的这个 .env 配置文件就可以实现不同的环境下运行相同的代码了。
通过 XDebug ,我们可以追踪到 env() 这个方法在底层调用了 vlucas 的 DotEnv 这个 Composer 组件来进行 PHPENV 类型文件的读写加载。
对于加载来说,在程序运行的时候,我们会通过下面这段代码来进行加载。
// laravel/framework/src/Illuminate/Foundation/Application.php foreach ($bootstrappers as $bootstrapper) { $this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); $this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]); }
其中核心是 $this->make($bootstrapper)->bootstrap($this); 这一段,它在循环中会加载所有 $bootstrappers 数组中的内容,这个数组里面的内容是在 laravel/framework/src/Illuminate/Foundation/Http/Kernel.php 中的类变量 $bootstrappers 所定义的。第一个环境变量启动加载器就是我们加载配置文件所需要的,如下所示:
// laravel/framework/src/Illuminate/Foundation/Http/Kernel.php /** * The bootstrap classes for the application. * * @var string[] */ protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ];
源码中和代码中的 Bootstrap 相关的内容都是启动加载器的实现,从文件名就可以看出,这个启动加载器是加载环境变量相关内容的。.env 文件里面的配置信息也将是以整体的环境变量的形式加载到系统中。
// laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php $this->createDotenv($app)->safeLoad();
LoadEnvironmentVariables.php 中会通过上述代码进入到 DotEnv 组件中,通过以下方法读取配置文件相关的信息。
// vlucas/phpdotenv/src/Dotenv.php create() // vlucas/phpdotenv/src/Loader/Loader.php load()
最后通过 ServerConstAdapter.php 文件中的 write() 方法将这些配置文件中的信息写入到 $_SERVER 全局变量数组中。
//vlucas/phpdotenv/src/Repository/Adapter/ServerConstAdapter.php /** * Write to an environment variable, if possible. * * @param string $name * @param string $value * * @return bool */ public function write(string $name, string $value) { $_SERVER[$name] = $value; return true; }
在路由文件中,我们可以通过打印 \$_SERVER 数据看到配置文件里面我们配置过的信息。之后的读取,也直接是读取这个 $_SERVER 中的数据。
Route::get('/', function () { var_dump($_SERVER); var_dump(env('REDIS_PASSWORD')); // null $_SERVER['REDIS_PASSWORD'] = '123456'; var_dump(env('REDIS_PASSWORD')); // string '123456' return view('welcome'); });
其实反过来看,我们的 Laravel 就是将 .env 文件中的数据缓存到了全局变量 $_SERVER ,然后我们在将来使用的时候就直接从全局变量中获取就可以了,这样就可以避免下一次还要从文件读取,从而提高系统效率。
一开始以为就是简单地讲讲目录和配置文件,没想到吧,直接就进入源码的分析了。当然,这只是开胃菜而已。对于框架架构的学习,一定要配置好 XDebug 之类的调试工具,如果没这些工具,这种使用了许多 Composer 组件来回调用的代码还真不好找出最终实现的地方。
后面的文章也都会以这样的方式进行,需要注意的是,我们的源码都是在 vendor 目录下的,所以有的文章中这个路径我就没有写了。
【相关推荐:laravel视频教程】
The above is the detailed content of Complete Laravel directory structure and configuration in half an hour. For more information, please follow other related articles on the PHP Chinese website!