3

I am currently trying to develop a custom Laravel Package, but is having some issues with getting started.

This is my file structure:

-packages
  - oliverbusk
   -invoiceconverter
    -src
      -controllers 
         - InvoiceconverterController.php
      -resources
         - views
           - home.blade.php
      -routes
         - web.php
    - InvoiceConverterServiceProvider.php

So as you can see, I have my files inside the src/ folder.

First of all, this is my composer.json, inside my package folder:

"extra": {
        "laravel": {
            "providers": [
                "Oliverbusk\\Invoiceconverter\\InvoiceConverterServiceProvider"
            ]
        }
    }

I have then autoloaded this in my projects main composer.json file:

"require": {
  //....
  "oliverbusk/invoiceconverter": "dev-feature-package"
},
"autoload": {
        [...]
        "psr-4": {
            "App\\": "app/",
            "Oliverbusk\\Invoiceconverter\\": "packages/oliverbusk/invoiceconverter"
        }
},

This is my serviceprovider file:

namespace Oliverbusk\Invoiceconverter;

use Illuminate\Support\ServiceProvider;

class InvoiceConverterServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //Load our routes
        $this->loadRoutesFrom(__DIR__ . '/routes/web.php');

        //Load our views
        $this->loadViewsFrom(__DIR__ . '/resources/views', 'invoiceconverter');
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
      //
    }
}

And my controller file, located in controllers/:

namespace Oliverbusk\Invoiceconverter\Controllers;

use App\Http\Controllers\Controller;

class InvoiceconverterController extends Controller
{
    public function index()
    {
        return view('invoiceconverter::home');
    }
}

Last, my routes/web.php file:

Route::group(['namespace' => 'Oliverbusk\InvoiceConverter\Controllers'], function () {
    Route::get('invoiceconverter', 'InvoiceconverterController@index');
});

Error :

Class Oliverbusk\InvoiceConverter\Controllers\InvoiceconverterController does not exist

Bonus info:

Composer dump autoload shows the following:

Discovered Package: oliverbusk/invoiceconverter
  • I have already tried to clear the cache with php artisan:cache:clear.
  • I have also tried composer update
9
  • try to publish the code php artisan vendor:publish Commented Aug 30, 2018 at 11:45
  • I'm not sure if it will make a difference but you should make sure your controllers directory is capitalised to match the namespace, so it should be Controllers. That could well be it though. Same goes for your class name and the file it is in, try to keep the same casing. Commented Aug 30, 2018 at 11:47
  • @urfusion - This package does not yet publish anything. Commented Aug 30, 2018 at 11:48
  • @Jonathon - just tried changing the folder name to Controllers - did not work. Commented Aug 30, 2018 at 11:48
  • Did you try changing the class name to match the name of the file, or the name of the file to match the class name? Commented Aug 30, 2018 at 11:49

1 Answer 1

1

The namespace in your route does not match the namespacing your have actually used.

Route::group(['namespace' => 'Oliverbusk\InvoiceConverter\Controllers'], function () {
    Route::get('invoiceconverter', 'InvoiceconverterController@index');
});

Change to

Route::group(['namespace' => 'Oliverbusk\Invoiceconverter\Controllers'], function () 
{
    Route::get('invoiceconverter', 'InvoiceconverterController@index');
});

And see if that helps.

I would also recommend you refactor all your code to be capitalised InvoiceConverter as they are two separate words.

Sign up to request clarification or add additional context in comments.

Comments

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.