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

Monday, March 7, 2022

[FIXED] Laravel package/service provider addition

 March 07, 2022     composer-php, laravel, package, service-provider     No comments   

Issue

I'm trying to use https://github.com/sebdesign/laravel-state-machine in my laravel application. But the package is not installed correctly. My controller doesn't recognise it. First, I added the following to my composer.json

"repositories": [
  {
     "type": "vcs",
     "url": "https://github.com/sebdesign/state-machine"
  }
]

Then I run

composer require sebdesign/laravel-state-machine:1.0

Then I added the following in my config/app.php

'providers' => [
    Sebdesign\SM\ServiceProvider::class,
],

'aliases' => [
    'StateMachine' => Sebdesign\SM\Facade::class,
],

After that I Publish the config file in config/state-machine.php using

php artisan vendor:publish --provider="Sebdesign\SM\ServiceProvider"

And that was it. Now I'm trying to use it in my controllers as:

// Using the facade
$stateMachine = StateMachine::get($article, 'simple');

But StateMachine is not recognised. I'm getting

Undefined type 'App\Api\V1\Controllers\Resource\StateMachine'

Am I missing something here? Should i add a use statement for something in the beginning of my controller?

Edit: Laravel 5.1 Thanks


Solution

You don't need to use following lines if you are using latest version and auto discovery tool.

Since version 5.5, Laravel uses package auto-discovery, so you don't need to manually add the ServiceProvider and the facade. If you don't use auto-discovery or you are using an older version, add the service provider and the facade in config/app.php.

<?php

'providers' => [
    Sebdesign\SM\ServiceProvider::class,
],

'aliases' => [
    'StateMachine' => Sebdesign\SM\Facade::class,
],

After composer require you need to directly run this command.

php artisan vendor:publish --provider="Sebdesign\SM\ServiceProvider"

You need to install compatible version of the package as per your laravel version.

composer require sebdesign/laravel-state-machine:^1.0

Then you need to use it as below:

use \SM\Factory\Factory as SMFactory;


Answered By - Dipak Mewada
  • 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