PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 18, 2022

[FIXED] Creating a custom helper in own package in laravel

 February 18, 2022     composer-php, laravel, php     No comments   

Issue

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": "alaeinia.majid@gmail.com"
        }
    ],
    "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?


Solution

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';
        }


Answered By - Majid Alaeinia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing