laravel 在laravel控制器中包含文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32163999/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:14:20  来源:igfitidea点击:

Include a file in laravel controller?

phplaravellaravel-5laravel-5.1

提问by Akshay Khale

I am creating a project with some additional functionality provided in form of a .php file API which contains some functions and some classes(out of which some class names are conflicting with Laravel built in class names) so, my question how should I include this file in my Laravel Controller to call functions in the file which using the classes of the file without referring Laravel classes and with less or no modification in .php API file?

我正在创建一个项目,其中包含一些以 .php 文件 API 形式提供的附加功能,其中包含一些函数和一些类(其中一些类名与 Laravel 内置的类名冲突)所以,我的问题应该如何包含这个文件在我的 Laravel 控制器中调用文件中的函数,这些函数使用文件的类而不引用 Laravel 类并且在 .php API 文件中进行较少或没有修改?

Note* I am using Laravel-5.1

注意*我使用的是 Laravel-5.1

采纳答案by Akshay Khale

Thank you all for taking efforts in trying to solve my problem but none of the solution worked for me so, here is what I tried for my problem

感谢大家努力解决我的问题,但没有一个解决方案对我有用,所以,这是我为我的问题尝试的方法

Below is the structure of my php file that I wanted to includes/integrate

下面是我想要包含/集成的 php 文件的结构

<?php

class Misc {

    const SUCCESS = 1;
    const FAILURE = 0;

    public static function get_hash ( $key )
    {
        ...
        ...
        ...
    }

    public static function show_reponse ( $result )
    {
        ...
    }

}

function check($keyhash) 
{
    ...
    ...
    ...
}

function function2() 
{
    ...
    ...
    ...
}

class Response {

    public function __construct ( $key )
    {
        ...
    }

    public function __destruct ()
    {
        unset( $this->key );
        unset( $this->params );
    }

    public function __set ( $key)
    {
        ...
    }

    public function __get ( $key )
    {
        return $this->params[$key];
    }

    private function check_now ()
    {
        ...
    }
}

The main problem I was facing is the class name Responsewhich was conflicting with Laravel Responseclass so I just removed all classes from the file and moved to their individual files in a new folder in Laravel\Appfolder and added namespaces to all classes.

我面临的主要问题是Response与 LaravelResponse类冲突的类名,所以我只是从文件中删除了所有类,然后移动到文件夹中新文件夹中的各个文件,Laravel\App并为所有类添加了命名空间。

Then I moved all functions in a PHP file in laravel\Appdirectory

然后我将 PHP 文件中的所有函数移动到laravel\App目录中

and used classnames along with the namespace defined and since I moved all functions in a different PHP file I could easily call the functions

并使用类名以及定义的命名空间,因为我将所有函数移动到不同的 PHP 文件中,所以我可以轻松调用这些函数

so here is my final folder structure of Laravel

所以这是我最终的 Laravel 文件夹结构

Laravel

Laravel

  -App
    -Console
    -Events
    -Exceptions
    -...
    -Libraries(Folder Containing Individual PHP files of classes from original file)
    -Providers
    -helpers.php(File containing all functions from original file)
    -User.php
  -bootstrap
  -...
  -...

回答by jedrzej.kurylo

If you have a custom file containing some classes/functions that need to be loaded for every request, you need to make sure it's added to the autoloader.

如果您有一个自定义文件,其中包含一些需要为每个请求加载的类/函数,您需要确保将其添加到自动加载器中。

In your composer.jsonadd the following in your autoloadsection:

在您的composer.json 中,在您的自动加载部分添加以下内容:

"autoload": {
  "files": [
    "path/to/your/File.php"
  ]
}

This will make sure the file is loaded. Now what you need is a way to use those classes without conflicting with existing Laravel classes.

这将确保文件已加载。现在你需要的是一种在不与现有 Laravel 类冲突的情况下使用这些类的方法。

First, make sure you have a namespace declaration at the top of your included file - say namespace Your\Namespace. In order to avoid conflicts, you need to explicitly tell PHP which class you mean when you reference it in the code. You mentioned your file contains a Responseclass that also exists in Laravel. In order to be able to use both, you need to alias one of them:

首先,确保在包含文件的顶部有一个命名空间声明 - 例如namespace Your\Namespace。为了避免冲突,你需要在代码中引用它时明确告诉PHP你指的是哪个类。您提到您的文件包含Laravel 中也存在的Response类。为了能够同时使用两者,您需要为其中之一设置别名:

use Illuminate\Http\Response as LaravelResponse;
use Your\Namespace\Response;

Now in your code you can refer to Laravel's Response class as LaravelResponse, and to your response by simply Response.

现在,在您的代码中,您可以将 Laravel 的 Response 类称为LaravelResponse,并通过简单的Response引用您的响应

Location of the file is irrelevant, as long as it's in a folder accessible to Laravel and its patch is added to composer.json.

该文件的位置无关紧要,只要它位于 Laravel 可访问的文件夹中,并且其补丁已添加到composer.json 中

Keep in mind that storing multiple classes per file is discouraged as a bad practice. I strongly suggest that you split your fine into separate file per class + one additional file with global functions.

请记住,不鼓励将每个文件存储多个类作为一种不好的做法。我强烈建议您将罚款分成每个类的单独文件 + 一个具有全局函数的附加文件。

回答by user2504370

Make an alias

取别名

Ex.

前任。

use App\Http\Requests\Request as DifferentRequest;

DifferentRequest->doStuff();

Aliasing/Importing

别名/导入

回答by Angel M.

make an alias as @user2504370 proposed,

使用@user2504370 提议的别名,

add to the composer:

添加到作曲家:

"autoload": {
    "classmap": [
        "database",
        "place_with_your_file",
    ],
    "psr-4": {
        "App\": "app/",
        "your_namespace": "your path",
    }
},

and run

并运行

composer dump-autoload

EDIT: there was a typo in classmap. I wanted to tell you you can put your file whenever you want, for example, you can create a new folder 'place_with_your_file', which is not necessarily inside Laravel's folder.

编辑:classmap 中有一个错字。我想告诉你,你可以随时放置你的文件,例如,你可以创建一个新文件夹“place_with_your_file”,它不一定在 Laravel 的文件夹中。

I'm using it with my external libraries.

我将它与我的外部库一起使用。

For PSR-4: if you are using namespaces, then here you will register the base namespace and the folder where can be found:

对于 PSR-4:如果您使用命名空间,那么您将在此处注册基本命名空间和可以找到的文件夹:

for example: "Utilities\\": "../utilities/app"

例如: "Utilities\\": "../utilities/app"

or whichever your path is.

或者无论你走哪条路。

and for classmap, you need to include path to this folder:

对于 classmap,您需要包含此文件夹的路径:

"../utilities/app"

and your autoload will look something like this:

您的自动加载将如下所示:

   "autoload": {
        "classmap": [
            "database",
            "../utilities/app",
        ],
        "psr-4": {
            "App\": "app/",
            "Utilities\": "../utilities/app"`
        }
    },