Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel 5中添加外部类_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 如何在Laravel 5中添加外部类

Php 如何在Laravel 5中添加外部类,php,laravel,laravel-5,Php,Laravel,Laravel 5,app/Libraries/TestClass.php中有一个类,包含以下内容: class TestClass { public function getInfo() { return 'test'; } } 现在,我想从控制器中的这个外部类调用getInfo()方法 我如何做这样的事情?首先,您应该确保该类位于正确的命名空间中。此处正确的命名空间应为: namespace App\Libraries; class TestClass { 然

app/Libraries/TestClass.php
中有一个类,包含以下内容:

class TestClass {
      public function getInfo() {
           return 'test';
      }
}
现在,我想从控制器中的这个外部类调用
getInfo()
方法


我如何做这样的事情?

首先,您应该确保该类位于正确的命名空间中。此处正确的命名空间应为:

namespace App\Libraries;

class TestClass {
然后,您可以像使用任何其他类一样使用它:

$test = new TestClass();
echo $test->getInfo();
不要忘记要在其中使用的类顶部的导入:

use App\Libraries\TestClass;

如果您无法控制名称空间或不想更改名称空间,请在
composer.json中的
classmap
中添加一个条目:

"autoload": {
    "classmap": [
        "app/Libraries"
    ]
}
然后运行
composer dump autoload
。之后,您将能够以与上面相同的方式使用它,除了使用不同(或没有)名称空间