trevorpan's avatar

Custom src/App directory is retrieved via app.php but not composer.json or vice versa

Hi,

Have an odd situation. It seems like composer has a different base directory than laravel. On the M1 chip with latest laravel version at v8.78.1.

<?php

namespace App;

class Application extends \Illuminate\Foundation\Application
{
    protected $namespace = 'App\';
}
// composer.json
    "autoload": {
        "psr-4": {
            "App\": "src/App",
            "Domain\" : "src/Domain/",
            "Support\" : "src/Support/"
        },
// bootstrap/App.php
use App\Application; 

$app = (new Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
))->useAppPath('src/App');
// Printout using valet and Ignition
Symfony\Component\Finder\Exception\DirectoryNotFoundException
The "src/App" directory does not exist. 

However, when adding .. to ->useAppPath('../src/App'); the site is served but when running composer dump-autoload to refresh where files are looked/served from I get this error from composer:

> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   Symfony\Component\Finder\Exception\DirectoryNotFoundException 

  The "../src/App" directory does not exist.

I've tried:

  • removing ..
  • removing ->useAppPath('../src/App');
  • removing "autoload": { "psr-4": { "App\\": "src/App", "Domain\\" : "src/Domain/", "Support\\" : "src/Support/" }, in various permutations but still they are not "lining up."

Have you run into this?

Is this a symfony issue?

0 likes
2 replies
trevorpan's avatar

Hi all,

Still trying to sort this out.

in Foundation/Application.php:

    /**
     * Set the application directory.
     *
     * @param  string  $path
     * @return $this
     */
    public function useAppPath($path)
    {
        $this->appPath = $path;

        $this->instance('path', $path);

        return $this;
    }

I cannot see why the above method would not override the default Laravel /../app folder.

Any ideas on how composer and bootstrap/app.php would be at odds with each other? It's like Laravel is trying to find this root folder in app\src\App ?

trevorpan's avatar
trevorpan
OP
Best Answer
Level 15

ok, got this cleared up. For anyone who may run into this. Just don't extend the Application class:

// bootstrap\app.php
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

$app->useAppPath($app->basePath('src/App'));

Please or to participate in this conversation.