Pergunta

I have a class which I would like to use inside of my Laravel App. Where do I place this and how do I integrate in Laravel workflow

what I tried

creating

/app/libraries
--myClass.php

than in composer

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/services",
        "app/facades",
                    "app/libraries",
        "public/site"
    ]
},

composer dump-autoload

Than I would like to initiate inside of a custom Model How would I do that?

under vendor/composer/autoloaded the class is present

than in my model I init autoloaded

public function MyMethod(){
      $instance = new MyClass();
      var_dump($instance);

      // pulled Error : Class 'App\Models\MyClass' not found 
}
Foi útil?

Solução

The work is almost done, now you have to make sure your class appears in

vendor/composer/autoload_classmap.php

Then you just need to use it anywhere:

class Post extends Eloquent {

    public function doThing()
    {
       $instance = new MyClass;
    }

}

If you are somehow using namespaces, you have to use them in the top of your class:

use MyClass;
use App\Classes\MyOtherClass;

class Whatever {
    public function MyMethod()
    {
      $instance = new MyClass();

      $other = new MyOtherClass();

      var_dump($instance);
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top