最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Custom built Laravel 11 package can't find layout - Stack Overflow

programmeradmin1浏览0评论

I'm writing a custom package in my Laravel 11 package. I have the following path to my package: packages/stsonline/sonar. Within this directory I have src. I'm having the following error with my layout file when trying to load up my "/sonar" route:

View [views.layouts.sonar] not found.

These are my files:

src/View/Components/SonarLayout.php

<?php

namespace Stsonline\Sonar\View\Components;

use Illuminate\View\Component;

class SonarLayout extends Component
{
    /**
     * Get the view / contents that represents the component.
     */
    public function render()
    {
        return view('sonar::layouts.sonar');
    }
}

resources/views/layouts/sonar.blade.php

@use('Stsonline\Sonar\Facades\Sonar')
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ $title ?? config('app.name', 'Laravel') }}</title>

    {!! Sonar::css() !!}
    @livewireStyles

    {!! Sonar::js() !!}
    @livewireScriptConfig
  </head>
  <body class="font-sans antialiased">
    <div class="lg:min-h-screen lg:flex bg-secondary-100">
      <main class="lg:pl-72 lg:flex-1 lg:flex lg:justify-center px-4 pt-6 md:pt-8 pb-4">
        <div class="container mx-auto space-y-8 md:px-4 pb-4">
          {{ $slot }}
        </div>
      </main>
    </div>
    {{-- @livewireScripts --}}
  </body>
</html>
@use('Stsonline\Sonar\Facades\Sonar')
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ $title ?? config('app.name', 'Laravel') }}</title>

    {!! Sonar::css() !!}
    @livewireStyles

    {!! Sonar::js() !!}
    @livewireScriptConfig
  </head>
  <body class="font-sans antialiased">
    <div class="lg:min-h-screen lg:flex bg-secondary-100">
      <main class="lg:pl-72 lg:flex-1 lg:flex lg:justify-center px-4 pt-6 md:pt-8 pb-4">
        <div class="container mx-auto space-y-8 md:px-4 pb-4">
          {{ $slot }}
        </div>
      </main>
    </div>
    {{-- @livewireScripts --}}
  </body>
</html>

This is how I'm loading the component:

$this->callAfterResolving('bladepiler', function (BladeCompiler $blade) {
    $blade->component('Stsonline\\Sonar\\View\\Components\\SonarLayout', 'sonar-layout', 'sonar');
});

My routes are working for the package, and the custom commands

here's my routes:

<?php

use Illuminate\Support\Facades\Route;

Route::name('sonar.page.')->group(function () {
    Route::view('/', 'sonar::pages.welcome')->name('welcome');
});

What am I missing?

SonarServiceProvider

<?php

namespace Stsonline\Sonar;

use Composer\InstalledVersions;
use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Queue\Events\Looping;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory as ViewFactory;
use Livewire\LivewireManager;
use RuntimeException;

/**
 * @internal
 */
class SonarServiceProvider extends ServiceProvider
{
    /**
     * Register any package services.
     */
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__.'/../config/sonar.php', 'sonar'
        );
    }

    /**
     * Bootstrap any package services.
     */
    public function boot(): void
    {
        if ($this->app->make('config')->get('sonar.enabled')) {
            $this->listenForEvents();
        }

        $this->registerAuthorization();
        $this->registerRoutes();
        $this->registerComponents();
        $this->registerResources();
        $this->registerPublishing();
        $this->registerCommands();
    }

    /**
     * Register the package authorization.
     */
    protected function registerAuthorization(): void
    {
        $this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
            $gate->define('viewSonar', fn ($user = null) => $app->environment('local'));
        });
    }

    /**
     * Register the package routes.
     */
    protected function registerRoutes(): void
    {
        $this->callAfterResolving('router', function (Router $router, Application $app) {
            if ($app->make(Sonar::class)->registersRoutes()) {
                $router->group([
                    'namespace' => 'Stsonline\Sonar\Http\Controllers',

                    'domain' => $app->make('config')->get('sonar.domain', null),
                    'prefix' => $app->make('config')->get('sonar.path'),
                    'middleware' => $app->make('config')->get('sonar.middleware'),
                ], function () {
                    $this->loadRoutesFrom(__DIR__.'/../routes/sonar.php');
                });
            }
        });


    }

    /**
     * Listen for the events that are relevant to the package.
     */
    protected function listenForEvents(): void
    {
        $this->app->booted(function () {
            $this->callAfterResolving(Dispatcher::class, function (Dispatcher $event, Application $app) {
                $event->listen(function (Logout $event) use ($app) {
                    if ($event->user === null) {
                        return;
                    }

                    // ...
                });
            });
        });
    }

    /**
     * Register the package's components.
     */
    protected function registerComponents(): void
    {
        $this->callAfterResolving('bladepiler', function (BladeCompiler $blade) {
            $blade->component('Stsonline\\Sonar\\View\\Components\\SonarLayout', 'sonar-layout', 'sonar');
        });

        $this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) {
            $middleware = collect($app->make('config')->get('sonar.middleware'))
                ->map(fn ($middleware) => is_string($middleware)
                    ? Str::before($middleware, ':')
                    : $middleware)
                ->all();

            $livewire->addPersistentMiddleware($middleware);
        });
    }

    /**
     * Register the package's resources.
     */
    protected function registerResources(): void
    {
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'sonar');
    }

    /**
     * Register the package's publishable resources.
     */
    protected function registerPublishing(): void
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__.'/../config/sonar.php' => config_path('sonar.php'),
            ], ['sonar', 'sonar-config']);

            $this->publishes([
                __DIR__.'/../resources/views/dashboard.blade.php' => resource_path('views/vendor/sonar/dashboard.blade.php'),
            ], ['sonar', 'sonar-dashboard']);

            $method = method_exists($this, 'publishesMigrations') ? 'publishesMigrations' : 'publishes';

            $this->{$method}([
                __DIR__.'/../database/migrations' => database_path('migrations'),
            ], ['sonar', 'sonar-migrations']);
        }
    }

    /**
     * Register the package's commands.
     */
    protected function registerCommands(): void
    {
        if ($this->app->runningInConsole()) {
            $this->commands([
                Commands\AboutCommand::class,
            ]);

            AboutCommand::add('Sonar', fn () => [
                'Version' => InstalledVersions::getPrettyVersion('stsonline/sonar'),
                'Enabled' => AboutCommand::format(config('sonar.enabled'), console: fn ($value) => $value ? '<fg=yellow;options=bold>ENABLED</>' : 'OFF'),
            ]);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论