如何在Laravel 5中添加我自己的自定义类?

Sid*_*ang 9 class laravel

好吧,在laravel 4中,如果我想添加自己的自定义类,例如:library\myFunction.php,那么我执行以下步骤:

  1. 将" myFunctions.php " 添加到app/library/myFunctiosn.php中
  2. app/start/global.php,在ClassLoader :: addDirectories(array(,我添加app_path().'/ library',
  3. 要在我的刀片视图中调用它,我添加以下代码
<?php
  $FmyFunctions1 = new myFunctions;
  $is_ok1=($FmyFunctions1->is_ok());   
?>
Run Code Online (Sandbox Code Playgroud)
  1. app/library/myFunctions.php的内容是:
<?php namespace App\library {

    class myFunctions {
        public function is_ok() {
            return 'myFunction is OK';
        }
    }

}
?>
Run Code Online (Sandbox Code Playgroud)

它有效.

但是如何在Laravel 5中这样做???

PS:我读过laravel 4帮手或基本功能的最佳实践和最佳场所是什么?

并尝试将"app/library /"添加到autoload数组并运行composer dum-autoload,但它一直给我错误:

xxxx行xx中的FatalErrorException:未找到类'myFunctions'

我也在尝试使用:

composer update
composer dump-autoload 
php artisan dump
php artisan clear-compiled
php artisan dump-autoload
php artisan optimize
php artisan route:clear
php artisan route:scan
php artisan route:list 
Run Code Online (Sandbox Code Playgroud)

但仍然无法正常工作......

Cap*_*row 19

应该对你有帮助.

仅供参考:基本上,您可以在应用程序中创建另一个目录,然后根据需要将文件命名为:

应用程序/ CustomStuff/CustomDirectory/SomeClass.php.

然后,在SomeClass.php中,确保将其命名为:

<?php 
namespace App\CustomStuff\CustomDirectory;

class Someclass {}
Run Code Online (Sandbox Code Playgroud)

现在,您可以使用类中的命名空间来访问此类:

使用App\CustomStuff\CustomDirectory\SomeClass;

  • 文件名和类名应该具有相同的名称。 (4认同)

Sid*_*ang 7

有时候经过反复试验,我找到了答案:

无需修改作曲家.只需将刀片修改为

<?php
  $FmyFunctions1 = new \App\library\myFunctions;
  $is_ok = ($FmyFunctions1->is_ok());
?>
Run Code Online (Sandbox Code Playgroud)