2
votes

I'm developing a Role-Permission package in Laravel and I want to use this package;

Laravel permission github

Problem is I can not use some functions in the main project when I install this package in my own package. example "HasRoles"

My packages composer.json file

 "require": {
        "spatie/laravel-permission": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "Modul\\Permission\\": "src"
        }
    },
    "extra": {
        "laravel": {
            "providers": [
                "Spatie\\Permission\\PermissionServiceProvider"
            ]
        }
    }

main project composer file

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^7.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Modul\\Permission\\": "packages/modul/permission/src"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

and my User model;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable;
    use HasRoles;

when I serve its show this error message...

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Trait 'Spatie\Permission\Traits\HasRoles' not found

What am I doing wrong here?

[1]: https://i.stack.imgur.com/KRUT0.png

1
Have you installed this package? If you just changed composer.json manually then you need to do composer update for Composer to install it into your vendor folder.d3jn
i use "composer require spatie/laravel-permission " commandEmre Canbulat
Are you serving laravel project that uses your package or some script that requires vendor/autoload.php?d3jn
maybe somthing happened before composer dumped its autoload files. try composer dump-autoload manually. just to be sureN69S
@EmreCanbulat one of your project's packages got updated. You need to pull those updates in for your project. Do composer update yourpackage/name (or composer update in general) for your project to bring in new changes and new dependency into it.d3jn

1 Answers

1
votes

The problem is you are just autoloading your packages files into your project. This way your project's composer doesn't know anything about your package's dependencies (hence why spatie/permission package is not being installed).

The correct way to do this is to require your package into your project. Usually you would create a repository for your project, register it at https://packagist.org as modul/permission and then run composer require modul/permission for you project.

But if your package is still not fully developed, I would recommend you require it not from packagist, but from the so-called path repository. Inside your project's composer.json add the following section:

{
...
    "repositories": [
        {
            "type": "path",
            "url": "packages/modul/permission"
        },
    ]
...
}

This will make composer look into your packages/modul/permission directory for your package when you require it. So do this and remove manual autoload of your package's source files from your project's composer.json (composer will use your package's autoload section to bind /src to Module\Permission namespace):

"psr-4": {
     "App\\": "app/",
     "Modul\\Permission\\": "packages/modul/permission/src" <--- remove this line
}

Lastly, run composer require modul/permission. Composer will find it inside the path repository we specified for him and symlink the packages/modul/permission directory to vendor/modul/permission and install it's dependencies as well.

Now you can edit your package inside packages/modul/permission folder. Once it's done be sure to publish it online on github/packagist, so that it can be remotely available from packagist repository to everyone, not just from your local path one.