dourao1896 2015-02-15 21:51
浏览 42
已采纳

将我的课程包括在Laravel 5项目中

I made a class in php with some helper methods that parse HTML files.

I'd like to use this class in my Laravel project, but I'm new to Laravel and it's not clear how to add a simple class to a Laravel 5 project.

Is this possible? Or do I need to go to all the trouble of creating a composer package for my class, hosting it somewhere, and then require it in my composer.json file. That seems like a lot of work for including a simple PHP class, and I'm hoping there's an easier way.

  • 写回答

2条回答 默认 最新

  • dsxon40042 2015-02-16 03:18
    关注

    As it stands right now there's not a great/easy way to do this in Laravel 5 (possibly by design). The two approaches you can take are

    Create a new class in the App namespace

    By default Laravel 5.0 looks for App\ prefixed classes in the app/ folder, so something like this should work

    #File: app/Helpers/Myclass.php
    <?php
    namespace App\Helpers;
    class Myclass
    {
    }
    

    and then create your class with

    $object = new App\Helpers\Myclass;
    

    This approach, however, relies on you creating classes in the App\ namespace, and there's some ambiguity around if the App\ namespace is owned by Laravel, or is owned by the developer of the application.

    Create your own Namespace and Register as PSR-4 autoloader

    A better, but more complicated, approach would be to create classes in your own namespace, and then tell Laravel about this namespace by registering a new PSR autoloader.

    First, you'd create the class definition

    #File: application-lib/Myclass.php
    <?php
    namespace Pulsestorm;
    class Myclass
    {
    }
    

    Notice we've created a new folder off the root folder to hold our classes named application-lib. You could name this folder anything you like, because in the next step, you're going to add a section to your composer.json file's autoloader section

    #File: composer.json
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Pulsestorm\\": "application-lib/"          
        }
    },
    

    The section we've added is this

    "Pulsestorm\\": "application-lib/"          
    

    The key to the object (Pulsestorm\) is your namespace. The value (application-lib) is the folder where composer should look for class definition files with the specified namespace.

    Once you've added this to composer.json, you'll need to tell Composer to regenerate it's autoload cache files with the dumpautoload command

    $ composer dumpautoload
    Generating autoload files
    

    After doing the above, you should be able to instantiate your class with

    $object = new Pulsestorm\Myclass;     
    

    The "real" right way to do this would be to create a generic composer package for your helper class, and then require that composer package into your laravel project. That may, however, be more work than you care to take on for a simple library helper.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)