search
Homephp教程php手册php Laravel 框架之建立后台文件夹 二

在前面的章节中我们讲解过如何在 Laravel框架中建立后台文件夹.

php Laravel 框架之建立后台文件夹


现在我们再添加一块内容.是关于自动加载的部分.

在我们app目录中还有个start目录.它里面这样写道:

In addition to using Composer, you may use the Laravel class loader to
load your controllers and models. This is useful for keeping all of
your classes in the "global" namespace without Composer updating.

我翻译一下:除了使用Composer,你还可以使用Laravel的类加载器来加载你的控制器和模型.

这对于让你的类在不使用Composer updating 的情况下处于全局的名字空间中非常有用.

我们这里有一个code fragment

ClassLoader::addDirectories(array(


app_path().'/commands',
app_path().'/controllers',
//app_path().'/controllers/admin',
app_path().'/models',
app_path().'/database/seeds',


));

对,很熟悉,这些文件夹都是我们经常用到的文件夹,控制器,命令.模型,数据库种子

这些文件夹中的内容就是可以自动加载到全局名字空间中的.

但是还多了一个我注释的内容.这个内容的目的是干什么呢..

就是让我们controllers目录中的admin中的控制器也同样具有全局的名字空间.这样我们就可以直接在路由表中通过

我们的控制器名字来接收了.

我测试了一下,确实成功了,而不再需要compoer dumpautoload

但是还是有个问题,如果admin文件夹中有一个控制器和外面的名字相同该怎么办?

我也同样做了一个实验,最终发现,它只会选择其中之一,至于到底选择哪一个,就看我们上面的那个加载的顺序了.

如果是

app_path().'/controllers',

app_path().'/controllers/admin',

那么外部的控制器有效,否则的话,admin文件夹里面的控制器有效.

结合之前写的这么多.其实最保险,也高大上的方法就是使用 名字空间.

但是编写起来也会更加复杂一点咯...小项目可以考虑我们的方法.大不了注意一下,别让这些控制器冲突就好了.

又不是没办法控制.对吧.

Do one thing at a time,and do well.

Best Wishes.


Statement
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment