Slim 3 autoloader

后端 未结 2 1686
面向向阳花
面向向阳花 2021-01-05 15:18

I\'m new to slim framework, and can\'t figure out how to use the autoloader to autoload my classes.

I created a app/models/myclass.php but of course wh

相关标签:
2条回答
  • 2021-01-05 15:46

    For autoloading of own classes you should use Composer by adding some options to your composer.json:

    {
        "require": {
            "slim/slim": "^3.9"
        },
        "autoload": {
            "psr-4": {
                "My\\Namespace\\": "src/"
            }
        }
    }
    
    // index.php
    require 'vendor/autoload.php';
    
    $app = new \Slim\App();
    $myClass = new \My\Namespace\MyClass();
    

    After running composer update composer will register your own namespaces and will autoload them for you.

    0 讨论(0)
  • 2021-01-05 16:02

    Add this in composer.json file Where app1 is the name of the folder you want to Autoload.

    "autoload": {
        "psr-4":{
            "app1\\": "anything"
        }
    }
    

    After doing this run this in cmd (via composer)

    composer dump-autoload -o
    
    0 讨论(0)
提交回复
热议问题