Randy_Johnson's avatar

Autoload SUCKS

I keep running into problems in auto load. I have created my own package structure which look like this

and I have three files, routes, controller, and Service Provider which seems to be the problem.

Service Provider

<?php

namespace Ecommerce\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class EcommerceServiceProvider extends ServiceProvider
{
    // ...
    protected function registerRoutes()
    {
        Route::middleware('web')
            ->namespace('Ecommerce\App\Http\Controllers')
            ->group(__DIR__ . '/../routes/web.php');
    }
}

Routes

<?php

use Illuminate\Support\Facades\Route;
use Ecommerce\App\Http\Controllers\EcommerceController;

Route::get('/ecommerce', [EcommerceController::class, 'index']);

Controller

<?php

namespace Ecommerce\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Inertia\Inertia;

class EcommerceController extends Controller
{
    public function index()
    {
        return Inertia::render('Ecommerce/Index');
    }
}

I don't know why this is a problem if I hard code the file address it gets around the problem but I don't want to do it this way. It even has AI stumped.

0 likes
8 replies
tykus's avatar

What is the issue exactly, the ->group(__DIR__ . '/../routes/web.php')?

Can you show the package's composer.json?

Randy_Johnson's avatar

I got it working, it flipping sucked *** but its working, so I can carry on from here. Module based packages using Inertiajs

Merklin's avatar

Are you creating a package or are you using a modular approach for a Laravel app? Because in both cases, your directory structure is not very conventional.

Usually, when creating a composer package for Laravel, composer.json and so on are in the root, and you have an src folder with the package classes, services and so on, but there is no need to have app. Meaning, you have src/database, src/Routes and so on inside the root folder of the package. Similar structure I have seen in a modular Laravel structure.

In brief, in the above tree, move everything under src to ecommerce and then rename app to src. Also, move database, resources and routes to the root folder (ecommerce). This way, your structure will resemble a Laravel application folder tree (except that app is named src).

daleconway's avatar

If this is a package, I think there might be a few issues..

It seems like you're trying the mimic application convention, and apply it to a library. It doesn't work like that. Not auto-magically, there still have to be hooks/hints.

The composer.json file, package.json, etc... if the root of your project is ecommerce and not src, they in wrong place.

Does the composer file hint at how to load the service provider. Does it have the right namespacing for PSR-4, it's easy to leave out a couple of \'s. I've done it lots.

Also, how did you add it to composer, did you have composer symlink local or use something like satis? If you just put it in the vendor folder, composer won't know it's there.

Also, @merklin response highlights perfectly the structure of a library, such as, composer in the root.

Randy_Johnson's avatar

Yes, I thought I wanted it as a package in the first place, but I tried a package called Laravel Modules, with Modules I was not able to get it to work with InertiaJS, so I created it myself so I knew what was happening under the hood.

I basically want it so I can create a module, and if a module is finsihed, I can easily copy that module over to another file (laravel app) for the customers need.

I will create the thing I made now as a laravel package, and call it laravel hot swap.

PS I am open to suggestions.

martinbean's avatar

I have three files, routes, controller, and Service Provider which seems to be the problem.

@randy_johnson You didn’t actually say what the problem was, though?

Please or to participate in this conversation.