Home » Code » Creating a Laravel 4 package

Creating a Laravel 4 package

Posted by on June 24th, 2013

Creating a Laravel 4 package
One of the best things about Laravel 4 is how it is built entirely around the idea of interoperability. For a long time, it was quite difficult to manage the dependencies of PHP projects. Copying and pasting code into projects was a nightmare, and keep things up-to-date was extremely difficult. Not many developers used PEAR and so you end up with each PHP project reinventing the wheel instead of using existing code from the community.

When choosing a PHP framework to work with you had to be fully committed to using only that framework because you couldn’t easily switch components out.

However with the rise in popularity of Composer (What is Composer?), PHP finally has a package manager that solves this problem.

Laravel 4 has been built from the ground up to work with Composer. Each of the components of the framework are actually individual dependencies that are brought together under one roof. This makes switching out components incredibly easy.

Laravel 4 also has built in tools for creating new packages as well as making it easy to leverage the beautiful facades implementation that allows you to create expressive syntax when working with your package.

In this tutorial I’m going to show you how to create your first PHP package using Laravel 4.

The Workbench

Laravel 4 makes creating new packages incredibly easy. In order for a package to work correctly, you need to set up a directory with the correct files and structure. This can be time consuming and so Laravel 4 can automatically generate these files for you using a single artisan command.

However, before you generate your new package, first you need to set up some configuration details. Open app/config/workbench.php and fill in your name and email. These details will be included in your package’s composer.json file.

Next we can generate the new package using the artisan command:

php artisan workbench vendor/package --resources

You need to replace the vendor and the package names with the names you want to use. Vendor is the name of your company, or organisation, or simply your name, and Package is the name of your package.

So for example, if I was creating a package called supyo I would run the following command:

php artisan workbench philipbrown/supyo --resources

The --resources flag tells artisan to create this package with some additional Laravel directories. If you are creating a package that can be used outside of Laravel, you can leave this off.

Package file structure

Now if you go into vendor/workbench you should see a new directory that contains your generated package.

Before you get started, remember to run composer install.

If you find yourself in a situation where you are getting a File not found error, but you are sure that the file is named correctly and you have the correct namespaces, its usually because composer isn’t aware of the file. To fix this you can simply run composer dump-autoload. This will regenerate the class maps of your package.

If you need to pull in other packages as dependencies, you simply add them to your composer.json file and run composer update like you would if you wanted to add a dependency to your main project.

Note: All of these commands should be run with your package directory, and not in the root of your project.

The file structure can seem a bit overwhelming at first, but you’ll begin to recognise it the more you start working with PHP packages.

The src directory is where you keep all your actual code for the package.

The tests directory is where you keep all your tests.

And the vendor directory is where all of your dependencies are stored.

You will notice there is also a composer.json. This is very important for making your package available on Packagist. This is where you require your dependencies so Composer can automatically pull them in.

Package Service Provider

Next if you drill down through the src directory, you should find a Service Provider file. Mine is:

/workbench/philipbrown/supyo/src/Philipbrown/Supyo/SupyoServiceProvider.php

You can think of the Service Provider file as kind of like this package’s individual bootstrap file. A bootstrap file is simply the file that sets everything up correctly during start up. Here we simply need to set a couple of things up so Laravel knows what to do with it.

Here is what your Service Provider should look like so far:

<?php namespace Philipbrown\Supyo;

use Illuminate\Support\ServiceProvider;

class SupyoServiceProvider extends ServiceProvider {

  /**
   * Indicates if loading of the provider is deferred.
   *
   * @var bool
   */
  protected $defer = false;

  /**
   * Bootstrap the application events.
   *
   * @return void
   */
  public function boot()
  {
    $this->package('philipbrown/supyo');
  }

  /**
   * Register the service provider.
   *
   * @return void
   */
  public function register()
  {
    //
  }

  /**
   * Get the services provided by the provider.
   *
   * @return array
   */
  public function provides()
  {
    return array('supyo');
  }

}

Notice how I have already filled in the details for the boot() and provides() methods.

Now you need to register the Service Provider in Laravel’s config. Open app/config/app.php and add your ServiceProvider to the bottom of the array:

'providers' => array(
  // --
  'Philipbrown\Supyo\SupyoServiceProvider',
),

Package Class

Next we can create the main class of this package. In the same directory as the SupyoServiceProvider.php file create a new file called Supyo.php:

<?php namespace Philipbrown\Supyo;

class Supyo {

  public static function greeting(){
    return "What up dawg";
  }

}

Notice how I set a namespace for this class.

Register the Service Provider

Next, back in the Service Provider, we need to register the new class with the Laravel’s IoC Container. I will probably fully explain how clever Laravel is and how the IoC container works in the future, but for now all you need to know is that you have to register your class so Laravel can resolve an instance of it.

To do that, we need to update the register method:

public function register()
{
  $this->app['supyo'] = $this->app->share(function($app)
  {
    return new Supyo;
  });
}

$this->app is just an array that holds all of the class instances. $this->app->share is a closure that will return an instance of your class. This means, when you try to use this package, it will be resolved using this instance from the IoC container.

Creating a Facade

Laravel uses a great syntax that makes writing code clean and elegant. Whilst on the surface, it seems like Laravel is just using a load of static methods, Laravel is actually resolving those classes out of it’s IoC container. If all of that went over your head, don’t worry, you don’t really have to understand what is going on. I will explore the in depths architecture and design patterns of Laravel in a future tutorial.

A facade allows you to use your class like this:

echo Supyo::greeting();

This is a facade because as you might already know, this is not how you would normally instantiate the class that we created earlier.

To create the facade, first create a new folder named Facades in your package directory. In this folder, create a new file called Supyo.php and copy the following code:

<?php namespace Philipbrown\Supyo\Facades;

use Illuminate\Support\Facades\Facade;

class Supyo extends Facade {

  /**
   * Get the registered name of the component.
   *
   * @return string
   */
  protected static function getFacadeAccessor() { return 'supyo'; }

}

Next add the following to the register method in your Service Provider class:

$this->app->booting(function()
{
  $loader = \Illuminate\Foundation\AliasLoader::getInstance();
  $loader->alias('Supyo', 'Philipbrown\Supyo\Facades\Supyo');
});

This allows the facade to work without the developer having to add it to the Alias array in app/config/app.php. Props to Chris Fidao for this.

Your package in action

Now you are all set to see your package in action.

Open up your routes.php and copy the following:

Route::get('/test', function(){
  echo Supyo::greeting();
});

Fire up the server and hit the test url, you should see the greeting printed to the screen.

Conclusion

This was a broad overview of how to create Laravel 4 packages. We’ve touched upon a couple of important areas of how Laravel works, in particular how the IoC allows Laravel to have a great syntax whilst still being very testable.

Don’t worry if some of the concepts in this tutorial went over your head. This was a tutorial on just setting up a new Laravel package. In the future I will go into much more depth to explain some of the elegant design patterns of Laravel and how it has been written to promote ease of testing whilst still maintaining it’s expressive syntax.

This is a series of posts on building an entire Open Source application called Cribbb. All of the tutorials will be free to web, and all of the code is available on GitHub.

To view a full listing of the tutorials in this series, click here.

Philip Brown

Hey, I'm Philip Brown, a designer and developer from Durham, England. I create websites and web based applications from the ground up. In 2011 I founded a company called Yellow Flag. If you want to find out more about me, you can follow me on Twitter or Google Plus.

  • jayweed

    tnx, that was 100% useful ;)

  • smronju

    Nice writeup thumbs up :)

  • Marcus Lenngren

    When going from one language and framework to another. These hands-on example tutorials together with the L4 Docs + source are invaluable. Great job, keep it up!

    • Thanks Marcus :D keep checking back as I continue this series! :)

  • James

    Thanks. I love Laravel but it’s difficult to get started. Code snippets in the documentation are too fragmented and lack cohesion. Thankfully there are tutorials like these to help us along. Keep up the good work!

  • yusida

    Hello there, thanks for the tutorial, I have try your code above, but I get error with the service provider. laravel said : Class ‘PhilipbrownSupyoSupyoServiceProvider’ not found. can u help me? I’m really new in laravel :D

    • Hmm, have you followed all of the steps above? I’m not sure why that would be happening.

      • yusida

        yes, I have followed all the steps but still got same error, can u maybe send your code to me? It may be help me, thanks :)

        • Email me your code and I’ll take a look

          • yusida

            https://www.dropbox.com/sh/t3ztmmqmng5kjbn/3JBwYlmFl3 this my code, I’ve shared it in dropbox :D, please check, thanks you.

          • Ok, so you are missing a couple of things. Have another read through the tutorial to see what you have missed. You will understand how Laravel packages work better if you find what you missed by yourself ;)

            The reason Laravel couldn’t find the Service Provider is because you need to run php artisan dump-autoload to regenerate your package’s autoload files.

            Have a read through the docs for pointers http://four.laravel.com/docs/packages

            If you can’t see what you have missed, give me a shout and I’ll point you in the right direction!

  • dataphile

    Why is your ServiceProvider called SupyoServiceProfile.php and not SupyoServiceProvider.php or are there two files?

    • Oops, good catch sir! That was my mistake, there should only be SupyoServiceProvider.php. Thank you!

  • mccombs

    I’m not able to get this to work. The error I’m getting is “Class ‘PhilipbrownSupyoFacadesSupyo’ not found”.

    I’ve run “php artisan dump-autoload” in my root directory. I’ve run “php composer.phar install” in the package directory and returned and installed ” – Installing illuminate/support (4.0.x-dev 6a690ae)”.

    What am I missing…?

    Also above you mentioned creating a file called “Supyo” in the same folder as SupyoServiceProvider but I assume you mean Supyo.php ?

    Side note: I’m able to return a view “return View::make(‘supyo::viewed’);” without issue.

    • Hmm, I’m not sure without looking through your code. Throw it on GitHub and I’ll take a look.

      Yes Supyo, should be Supyo.php. I’ll update the text because that is confusing, thank you!

  • Pingback: Creating flexible Controllers in Laravel 4 using Repositories | Culttt()

  • Dmiller

    Is it possible to extend Eloquent within your facade?

    • Yeah, you would just make a new class that extends Eloquent and then create a Facade for it. I’ve actually got a post on exactly that coming soon, so check back for it!

  • Dominique de Graaff

    “To create the facade, first create a new folder named Facades in your package directory”

    Where is this ‘package’ directory I have no ‘package’ directory.

    It feels like i have to run to china and back to create a simple ‘module’ for handling application settings. It is so bloated. Files spread al over the place. What’s the advantage of this extremely bloated serviceprovider pattern?

    I like the migrate/seeder and the orm of laravel but creating packages has become a total nightmare.

    • By “package” (no capital letter) I mean whatever you called your package. So my Facades folder would be under PhilipbrownSupyoFacades. You just have to go off the namespace if you are unsure.

      The directory structure is a standard for PSR loading. It’s how you can automatically load all your packages without having to do anything. If you look through an modern PHP package you will see they all follow this simple structure.

      The ServiceProvider pattern allows you to very easily bootstrap your package into Laravel.

      I can appreciate that it might seem overwhelming at first, but after you have made a couple of packages it becomes really easy. You just have to understand what bits you need. You can create a package using Laravel’s Workbench very quickly.

      • Dominique de Graaff

        Thanks for your excellent reply.

  • Dominique de Graaff

    I understand the construction but after i ran composer update from my laravel root folder i got this really anoying error which doesn’t explain me much:

    Class 'QsolSettingSettingServiceProvider' not found

    I’ve used the composer dump-autoload.
    What am i doing wrong here? I’m pulling my hair out.

    The repository is here:
    https://github.com/Nique/svborger

    • Dominique de Graaff

      Nevermind, i figured it out.

      The composer.json file in my package was corrupt (it was set with a forward slash instead of 2 backwardslashes)

      "psr-0": {

      "Qsol\Setting": "src/"

      }

  • Janne Kurkinen

    Hey Phill! Care to look into my package, I’m also getting ‘Class not
    found’? I have tested loads of combinations with lower case vs capitals
    and so on…

    This package is going to be named Html2Pdf – it’s a
    port of a good library for generating PDF files with HTML. You can
    download my current workbench folder here: http://ge.tt/6wRYNjp

    In addition I have these lines in the app config, at the respective locations:
    ‘EnvisioPdfPdfServiceProvider’
    ‘Pdf’ => ‘EnvisioPdfFacadesFacade’

    • Have you tried running composer dump-autoload? If you are sure that everything is spelt correctly it’s usually just because composer doesn’t know about the file.

      • Janne Kurkinen

        I have ran dumpautoload after every change on namings. I’m suspecting that Composer doesn’t for some reason find the bench/serviceprovider.

        As I updated my post, is it usual that Composer installs illuminate/support under package/vendor folder?

        I have more info and a new workbench project shared in my comment on Víctor’s post, on the top of this post.

        • Janne Kurkinen

          Needed to run ‘composer update or install’ in the package dir, not the main dir!

          • Glad you got it sorted :) I’m going to update this tutorial to be more clear.

  • Víctor Hernández

    For the people with the “Class not found” error:
    When you run the php artisan workbench … command and sees the composer update message please delete and run the command again because the package is installed but is broken without some components.

    • Janne Kurkinen

      How is it broken? I have started from the scratch several times and have followed these tutorials, used installed packages as examples and have used workbench command as bootstrap. I can’t seem to find the bug, I’m getting: Class ‘EnvisioHtmlpdfHtmlpdfServiceProvider’ not found

      Here is my newest bench which should be already working by the guidelines: http://ge.tt/4xvTgnp/v/0

      • neychang

        i think ‘not find class’ prolem just because forget type ‘composer update’ in package directory

        • Janne Kurkinen

          There you had it neychang! How come none of the four tutorials I was following mentioned this… JasonLewis seems to have written ‘Remember to run composer install first’ … I did so, but in the main directory. Thanks a lot! :)

          • Hey @envisio:disqus Janne, could you have a quick read through the section “Package file structure”? Is that good explanation of the problems you were facing and how to resolve them?

            Thank you :)

          • Janne Kurkinen

            That description seems pretty good intro as it’s now updated. Anyways one needs to study some released packages as examples along with artisan generated boilerplate. And those file structures do vary which might be difficult to grasp at first, as Composer is also in the play here.

            I’m still in early learning phase with package development. This article series made me to realize there is much potential with developing your app even totally in workbench environment: http://kirkbushell.me/laravel-4-an-alternative-application-workflow-part-2/

            Even an entire article could be written to study and compare different strategies for organizing your app file structure. For example Kirk has common controllers folder directly in src/ folder, shared along those packages…

          • Absolutely! I think it would be possible to break each of these posts down into more in-depth guides on each of the aspects.

            At least I will never run out of content ideas :)

          • Janne Kurkinen

            Bring on! :)

          • neychang

            sorry for later! keep learning on laravel with you !!

        • Janne Kurkinen

          Is it a common practice for Composer to install the default dependency illuminate/support under vendor/ folder in /workbench/me/mypackage?

  • Rups

    What am I missing? Still get the response “Class ‘MyNameCoreFacadesCore’ not found” when I add the following return for a route: “return Core::greeting(); “

    • If you are 100% sure that you have everything spelt correctly, try running php artisan dump-autoload and composer dump-autoload.

      • Carlos Arturo Alaniz

        I’m having the same issue, did you solved it?

        • Johan Nyberg

          Carlos, where do you add the package path in the composer.json?

          This is my composer.json:

          {
          “name”: “jnyberg/testo”,
          “description”: “”,

          “authors”: [
          {
          “name”: “Johan Nyberg”,
          “email”: “nybergjohan@gmail.com”
          }
          ],

          “require”: {
          “php”: “>=5.3.0”,
          “illuminate/support”: “4.1.*”
          },

          “autoload”: {
          “psr-0”: {
          “Jnyberg\Testo”: “src/”
          }
          },

          “minimum-stability”: “stable”
          }

    • Johan Nyberg

      I still have the exact same problem! Many people here seem to run into this error of the facade class not being found…

    • marco di giovambattista

      When Philip said To create the facade, first create a new folder named Facades in your package directory. In this folder, create a new file called Supyo.php and copy the following code:

      you need to create this directory under /workbench/vendor/package/src/Vendor/Package/ then it works

  • Almeida

    Ahha, had to `composer dumpautoload` in the package directory!

  • shaneferguson

    Anyone getting the error “Fatal error: Cannot redeclare crypt_random_string()”? Been through it a couple of times now everything seems correct, I dont understand why i would be getting this error?

    BTW great tutorial so far Philip Brown :)

    • Thank you :)

      Hmm that’s weird, in what context are you getting it?

      • shaneferguson

        I get it when i do composer install, or even when i go to /test at the end of this section.
        Full error is:
        Fatal error: Cannot redeclare crypt_random_string() (previously declared in /var/www/vendor/phpseclib/phpseclib/phpseclib/Crypt/Random.php:61) in /var/www/workbench/philipbrown/supyo/vendor/phpseclib/phpseclib/phpseclib/Crypt/Random.php on line 248

        • Hmm I guess your problem must be phpseclib, I’d perhaps start from scratch and step through the process step-by-step to find the problem.

          • shaneferguson

            Got it working. I guess it must of been a now fixed issue with a dependancy because after doing another composer update it works.

          • Excellent Shane, glad to hear it :) Yeah those kinds of bugs can be a right pain in the arse to resolve :( been there, done that.

  • Myregistration

    Is there a Laravel command to uninstall a package? Thanks for the great tutorials!

    • Hmm, no I don’t think so. It would probably best to just delete the directory.

  • myregistration

    Which directories are created for the package when you use the –resources flag? What are the advantages and disadvantages? Do you have a link to more info in regards? If we want to create a library of custom classes for a project are packages the way to go or overkill? The classes are specific to the project and not meant to be shared outside our project. Thanks again!

    • The Laravel docs is a good place to start to learn more (http://laravel.com/docs/packages#creating-a-package). By using the --resources flag you will also have directories for migrations, views, config.

      You could do it as packages, but if I’m writing a library for a specific project I usually just create an project specific namespace and keep all of my code under there.

  • Alessio

    hi, i have this error

    Symfony Component Debug Exception FatalErrorException
    Class ‘PhilipbrownSupyoSupyoServiceProvider’ not found

    i tried to run php artisan dump-autoload, but I encountered this error

    PHP Fatal error: Class ‘PhilipbrownSupyoSupyoServiceProvider’ not found in /Applications/AMPPS/www/laravel2/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 123

    {“error”:{“type”:”Symfony\Component\Debug\Exception\FatalErrorException”,”message”:”Class ‘Philipbrown\Supyo\SupyoServiceProvider’ not found”,”file”:”/Applications/AMPPS/www/laravel2/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php”,”line”:123}}{“error”:{“type”:”Symfony\Component\Debug\Exception\FatalErrorException”,”message”:”Class ‘Philipbrown\Supyo\SupyoServiceProvider’ not found”,”file”:”/Applications/AMPPS/www/laravel2/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php”,”line”:123}}

    how can i fix it? i tried on a clean version of laravel

    • Laravel can’t find your Service Provider, so you either have your namespace wrong or you composer can’t find it.

      Check to see if your namespaces are correct. Then try composer dump-autoload. It’s one of those two things.

  • Yail A. Anderson

    Hi thanks for the post, i want to remove or uninstall a package but i don’t find any info about that

    • Hmm, it’s probably best to just delete the directory. I don’t really know of a more elegant solution.

  • Nderim Rahmani

    ErrorException

    Class ‘PhilipbrownSupyoFacadesSupyo’ not found

    return class_alias($this->aliases[$alias], $alias);

    • Have you got all the filenames spelled correctly?

      • Peter Furesz

        Thank you Philip for the response. Your code is right!

        I found the problem: the getFacadeAccessor() method must need to return the lowercased version of the class name. I used the “exact” class name, but as soon as I lowercased, everything ok now.

        Thank you for the great tutorials, just start to learn Laravel a few hours ago (moving from CodeIgniter)

    • Peter Furesz

      I am in the same situation. I think there is a mistake in the example, so Philip, please double check it.
      We have two Supyo.php, one in the Facades directory and one in the same directory where the SupyoServiceProvider.php located.

      We created a static function in the main Supyo class (not in what extends the Facade).

      In the register method you write, use the following code:

      $loader->alias(‘Supyo’, ‘PhilipbrownSupyoFacadesSupyo’);

      but I think, the correct is

      $loader->alias(‘Supyo’, ‘PhilipbrownSupyoSupyo’);

      After I change it everything is working as expected.

  • Johan Nyberg

    Thanks again for a great tutorial!

    Maybe you’re a bit vague at the end:
    “Next add the following to the register method in your Service Profile file:”

    I figured out you meant the ServiceProvider class, and from reading Chris Fidao’s post I put it in the register-class. Maybe you should point that out in your tut.

    But alas, I still get the “Class ‘JnybergTestoFacadesTesto’ not found” exception.

    I’ve run “composer dump-autoload” in the package dir and “php artisan dump-autoload” in the app root.

    So close, but no cigar :)

    • Ah yes, that is a mistake. I’ll fix it, thank you.

      Are you 100% sure that you’ve got all your class names, namespace and file names correct? Whenever I get that error it’s nearly always just that.

      • Johan Nyberg

        Yup, checked my source code again for the fourth time or something, everything correct. Still get

        Class ‘JnybergTestoFacadesTesto’ not found

        Done php artisan dump-autoload in cribbb folder, and composer dump-autoload in package folder.

        • The Facades file should be under the srcJnyBergTesto directory and it should start with a capital letter.

          "Jnyberg\Testo": "src/" in your composer.json file is telling composer how to autoload your files. So the directory needs to be in the correct place or composer won’t know how to load it.

          • Johan Nyberg

            Thanks again Philip for taking your time! It’s amazing to follow along in a blog tutorial series like this and have someone as dedicated as you take your time to look at my newbie code. It’s like having a private tutor! Thank you.

            I got it sorted. I took you a bit to literally:

            “To create the facade, first create a new folder named Facades in your package directory.”

            …and created the facades directory directly under the package directory. But of course, as you write in your tutorial, all source code of the package should be under the src directory.

            Thanks again for a great tutorial!

          • No problem Johan :)

            Yeah, to be honest I think I need to be clearer in my writing. It’s easy to slip into making assumptions and not explaining exactly what I mean :(

  • Rizwan

    How do i get the global App object in package class?

    • If you need to have access to App you shouldn’t be making a package, it needs to be part of your project.

  • superzamp

    Within the service provider class, isn’t putting code in the $this->app->booting closure equivalent to putting it in the public function boot ?

    Concise and helpful, thanks !

    • Hmm, I’m not sure. I usually just deferred stuff in the boot() method.

  • Fisherman

    Thanks, That was helpful.

  • Nirmal Thapa

    whoa ! did I just create my very own package. Its starting to get more and more interesting.
    Thanks Philip

  • Umanda Jayobandata

    Hi Philip;

    Grate and helpful tutorial. Thank you very much and your code is working perfectly.

    I have a small concern, let say I have a my package but I don’t want to publish it publically but selected people. lets say my development team.

    imaging I have privet git repository in GitHub or Bit Bucket.

    So Question is how I publish my package over the internet. ?

    I have searched over internet and found this. I don’t know this is correct or net. I am very glad if you can give your suggestion.

    {
    “name”: “laravel/laravel”,

    “require”: {
    “laravel/framework”: “4.0.*”,
    “vendor/laravel-test-pack”: “dev-master”
    },
    “repositories”: [{
    “type”: “git”,
    “url”: “git@bitbucket.org:user/laravel-test-pack.git”
    }],

    }

    Thank you very much
    Cheers & Happy Coding.!

  • 尤川豪

    thank you very much!
    this is a great article!
    awesome!

  • borgulas

    I had a problem too with the Class not being found at first, followed another tut and then found out I had registered the ServiceProvider name wrong in my app.php file (copied and pasted the file name and didn’t remove the ‘.php’ part). You might want to check this if you run into the error.

  • Adi

    Hi Philip, great article again…

    however, houston we have a problem..

    followed the guide step by step to create eddywebsdbtoapi package.

    however calling it with dbtoapi::index() throws an error (I am calling the greeting() method as index() )

    Class ‘eddywebsdbtoapiFacadesdbtoapi’ not found

    no idea why its doing that, github url for this package is: https://github.com/eddywebs/dbtoapi-package

    I would really appreciate any pointers,

    cheers !

    • Thank you :)

      Well that means Composer can’t find your file. That is because either you have a spelling mistake in your file names or namespaces.

  • Do you know how i can add my L5(https://github.com/alirezarahmani/CreateLaravel-5-package ) package to packagist, It shows me error:
    The package name Alireza Rahmani – Laravel 5 Package is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match “[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*”.

  • vince

    I’m trying to get my head around what a Service Provider is, and found your article. Is a Service Provider synonymous with a Package or is it a feature of a package?

    Secondly, if I’m building an app, should I be building my app as a collection of SP’s rather than as a collection of classes ?? If so why ??

    Apologies, I come from Codeigniter where everything seems so much simpler. Many Thanks !

    • A Service Provider is how you bootstrap your classes. That might involve registering it in the IoC container, or booting some method.

      You don’t have to build specifically around the idea of Service Providers, you just use Service Providers whenever you need to bootstrap stuff.

  • Daniel Manji Chikaka

    You can have your own route file for a package. Just make a new file named routes.php and add use IlluminateSupportFacadesRoute; at the top. Don’t forget to register the route file in service provider file (package service provider) by including it like so : include __DIR__.’/../../routes.php’; in the register method. That’s all, hope it gonna help someone ;)

  • chagam kamalakar

    Thanks phil…

  • Interuptic

    For those still making Laravel 4 packages, I came across 2 problems in this tutorial:

    1) Running the “php artisan workbench” command to generate the files works, but also threw this error “sh: composer: command not found”

    This is because artisan assumes your composer executable is in the ~/bin directory, whereas my composer.phar file is located in /usr/local/bin

    See https://github.com/laravel/framework/issues/5174

    I don’t know if this affects however the process..

    2) Composer install must be run from within workbench/[vendor]/[package]. Otherwise your service provider won’t be found, even if you do the dump autoload and name everything correctly.

    See http://stackoverflow.com/questions/18499393/laravel-4-workbench-class-not-found#18574832