How can I autoload a custom class in Laravel 5.1?

前端 未结 4 1450
栀梦
栀梦 2021-02-08 21:34

I\'ve created a library folder within the app folder to add my own classes.

This is the content of the file app/library/helper.p

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-08 21:46

    I know this question was answered a while ago, but the reason it isn't working is that you need to give the namespace that corresponds with the file structure. Therefore, since the Library class is inside the App folder you need:

    namespace App\Library;
    
    class MyHelper{
        public function v($arr){
            var_dump($arr);
        }
    }
    

    Additionally, if you are going to call the class MyHelper, you need to call the file MyHelper.php

提交回复
热议问题