在 laravel 5.2 中创建新类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36530306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create new classes in laravel 5.2
提问by Alexey Mezenin
I'm working with Laravel framework.
我正在使用 Laravel 框架。
I have a question regarding classes creation. To explain my question, i will give you an example:
我有一个关于创建类的问题。为了解释我的问题,我会给你一个例子:
class articleController extends Controller{
public function bla(){
$a = new MyNewClass($colorfulVariable);
$b = $a->createSomething(new MySecondNewClass());
}
}
Now, the classes MyNewClass
and MySecondNewClass
do not exist, I want to create them, use them in every controller I want, just like I use the Redirect or Request classes that Laravel
offers.
现在,这些类MyNewClass
和MySecondNewClass
不存在的,我想创建它们,在每个控制器我想使用它们,就像我使用重定向或请求类,Laravel
优惠。
How can i create this classes? Make a new functions to use them in my laravel project?
我怎样才能创建这个类?创建一个新函数以在我的 Laravel 项目中使用它们?
回答by wezzy
you can just create those classes in your app folder (or in subfolder for example we create a Libraries folder with all the utilities). Just include a use App\YourClass; before using those classes and laravel will include the classes for you
你可以在你的 app 文件夹中创建这些类(或者在子文件夹中,例如我们创建一个包含所有实用程序的库文件夹)。只需包含一个 use App\YourClass; 在使用这些类之前,laravel 将为您包含这些类
回答by Shapon Pal
Laravel 5.* is autoloaded using PSR-4, within the app/ directory. You can see this in the composer.json file.
Laravel 5.* 使用 PSR-4 自动加载,位于 app/ 目录中。您可以在 composer.json 文件中看到这一点。
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\": "app/"
}
},
Basically, you could create another directory within app, and then namespace your files in there as appropriate:
基本上,您可以在 app 中创建另一个目录,然后根据需要在其中命名您的文件:
app/folder1/folder2/SomeClass.php.
Then, within your SomeClass.php, make sure you namespace it:
然后,在你的 SomeClass.php 中,确保你命名它:
<?php namespace App\folder1\folder2;
class Someclass {}
Now, you can access this class using the namespace within your classes:
现在,您可以使用类中的命名空间访问此类:
use App\folder1\folder2\SomeClass;
回答by Alexey Mezenin
You can create your classes as usual, but don't forget to include them into composer.json
autoload section:
您可以像往常一样创建类,但不要忘记将它们包含在composer.json
自动加载部分中:
"autoload": {
"classmap": [
"App\MyClassesNamespace"
....
And then run composer dumpauto
command to rebuild autoloader file. If you'll not do this, you'll not be able to use your classes in your application.
然后运行composer dumpauto
命令来重建自动加载器文件。如果您不这样做,您将无法在您的应用程序中使用您的类。
回答by Ankur Soni
You don't have to do anything other than below things:
除了以下事项外,您无需执行任何其他操作:
- Create a class in App\ folder.
- Whenever you use the class, you will have to import the class using "use App\XYZ;" on top of the importing class.
- 在 App\ 文件夹中创建一个类。
- 无论何时使用该类,都必须使用“use App\XYZ;”导入该类。在导入类之上。
thats it. Mostly people follow convention and then create "Service" folder on App\ folder. It is good to create alien class into service folder which are other than laravel framework classes.
就是这样。大多数人遵循惯例,然后在 App\ 文件夹上创建“服务”文件夹。最好将外星人类创建到除 Laravel 框架类之外的服务文件夹中。