3

I am writing a new composer package to be used in laravel, in which I want to define my own helper function (the helper is defined in my own module, not in the laravel, but to be used by laravel - see the helpers.php in the tree below).

This is the packages folder tree of my module in the root of a fresh laravel project:

└───majidalaeinia
    └───favicon
        │   composer.json
        │
        ├───src
        │   │   FaviconServiceProvider.php
        │   │
        │   ├───app
        │   │       helpers.php
        │   │
        │   └───routes
        │           web.php
        │
        └───vendor
            │   autoload.php
            │
            └───composer
                    autoload_classmap.php
                    autoload_files.php
                    autoload_namespaces.php
                    autoload_psr4.php
                    autoload_real.php
                    autoload_static.php
                    ClassLoader.php
                    installed.json
                    LICENSE

Here is the majidalaeinia/favicon/composer.json content:

{
    "name": "majidalaeinia/favicon",
    "description": "This is an educational package on favicon.",
    "license": "MIT",
    "authors": [
        {
            "name": "Majid Alaeinia",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "psr-4": {
            "majidalaeinia\\favicon\\": "src/"
        },
        "files": [
            "src/app/helpers.php"
        ]
    }
}

And here is the composer.json of the laravel project itself:

// ...
"autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/",
            "majidalaeinia\\favicon\\": "packages/majidalaeinia/favicon/src"
        }
    },
// ...

I have defined a helper in majidalaeinia/favicon/src/app/helpers.php and have tried composer dumpautoload command on laravel route and my own project and get no error, but can not see the result of my helper which is a simple dd() right now.

I get this error:

Call to undefined function testtt() (View: D:\projects\favicon\resources\views\welcome.blade.php)

What can I do to use my custom helper in my module in laravel project?

8
  • When you dumpautoload does composer give you any errors? One thing to try is to delete the composer.lock file and run composer install again and see if your helper module gets brought in. This should rebuild everything missing in your vendor directory. Commented Aug 24, 2019 at 8:02
  • @RyanNerd No errors, runs successfully. Commented Aug 24, 2019 at 8:06
  • 1
    What happens if you call your function with the complete namespace to \majidalaeinia\favicon\testtt()? Commented Aug 24, 2019 at 8:17
  • @mdexp I have tried that already, returns Call to undefined function majidalaeinia\favicon\testtt() (View: D:\projects\favicon\resources\views\welcome.blade.php). Commented Aug 24, 2019 at 8:19
  • Based on your psr-4 I think you should use this path: packages/majidalaeinia/favicon/src/app/helpers.php or majidalaeinia/favicon/src/app/helpers.php Commented Aug 24, 2019 at 8:22

1 Answer 1

4

Could not solve it by composer independently and used my package service provider. I added this code to the boot() method of my FaviconServiceProvider and it works now:

if (File::exists(__DIR__ . '\app\helpers.php')) {
            require __DIR__ . '\app\helpers.php';
        }
Sign up to request clarification or add additional context in comments.

2 Comments

where is the file path you have put?
That's great! For the lazy user that just copy and paste: Replace the \ with / to get it working ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.