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

标签 class laravel

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

  • 将“ myFunctions.php ”添加到应用程序/库/myFunctiosn.php
  • 应用程序/开始/global.php ,内 ClassLoader::addDirectories(array( , 我添加 app_path().'/library',
  • 为了在我的 Blade View 中调用它,我添加了以下代码

  • <?php
      $FmyFunctions1 = new myFunctions;
      $is_ok1=($FmyFunctions1->is_ok());   
    ?>
    
  • app/library/myFunctions.php 的内容是:

  • <?php namespace App\library {
    
        class myFunctions {
            public function is_ok() {
                return 'myFunction is OK';
            }
        }
    
    }
    ?>
    

    它有效。

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

    PS:我读了What are the best practices and best places for laravel 4 helpers or basic functions?

    并尝试添加 "应用程序/库/", 自动加载数组 并运行 composer dum-autoload ,但它一直给我错误:

    FatalErrorException in xxxx line xx: Class 'myFunctions' not found



    我也已经在尝试使用:
    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 
    

    但是还是不行...

    最佳答案

    This应该可以帮助你。

    供引用:
    基本上,您可以在 app 中创建另一个目录,然后根据需要在其中命名您的文件:

    app/CustomStuff/CustomDirectory/SomeClass.php。

    然后,在你的 SomeClass.php 中,确保你命名它:

    <?php 
    namespace App\CustomStuff\CustomDirectory;
    
    class Someclass {}
    

    现在,您可以使用类中的命名空间访问此类:
    use App\CustomStuff\CustomDirectory\SomeClass;
    

    关于class - 如何在 Laravel 5 中添加我自己的自定义类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942390/

    相关文章:

    php - 如何解决跨源laravel-echo-server错误?

    laravel - 在 vue.js laravel 中如果值为 null 则隐藏 div

    laravel - 我可以将 Route-Model-Binding 的解析逻辑更改为始终小写字符串键吗?

    laravel - 延迟()通知似乎不起作用

    ubuntu - 无法在 ubuntu 上使用 laravel 框架创建项目

    CSS类不工作

    c++ - C++类中的指针数组

    C#:不可变类

    php - 函数内声明函数的作用域

    python - Python 方法名称末尾的括号是什么?