lunarphp / lunar

An open-source package that brings the power of modern headless e-commerce functionality to Laravel.
https://lunarphp.io
MIT License
2.64k stars 344 forks source link

Changing panel url not changes routes for custom resources #1764

Closed morawcik closed 4 months ago

morawcik commented 4 months ago

I changed panel url from lunar to hub to stick with what I had with 0.8 and then I created custom resource:

LunarPanel::panel(function ($panel) {
    return $panel->path('hub')
        ->resources([
            BannerResource::class,
        ]);
})->register();

and I'm getting error Route [filament.lunar.resources.banners.index] not defined. But when I'm keeping path as lunar then it works fine:

LunarPanel::panel(function ($panel) {
    return $panel->path('lunar')
        ->resources([
            BannerResource::class,
        ]);
})->register();
glennjacobs commented 4 months ago

I don't believe this is a Lunar issue as I have this working fine locally. There will likely be something wrong with your custom Resource.

Can share your code?

morawcik commented 4 months ago

That's my resource:

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BannerResource\Pages;
use App\Models\Banner;
use Filament\Forms\Components\Component;
use Filament\Forms\Form;
use Filament\Tables;
use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;
use Filament\Tables\Table;
use Lunar\Admin\Support\Forms\Components\Attributes;
use Lunar\Admin\Support\Resources\BaseResource;
use Lunar\Admin\Support\Tables\Columns\TranslatedTextColumn;

class BannerResource extends BaseResource
{
    protected static ?string $model = Banner::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function getLabel(): string
    {
        return 'Banner';
    }

    public static function getPluralLabel(): string
    {
        return 'Banners';
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                static::getAttributeDataFormComponent(),
            ])->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                SpatieMediaLibraryImageColumn::make('thumbnail')
                    ->collection('images')
                    ->conversion('small')
                    ->limit(1)
                    ->square()
                    ->label(''),
                static::getNameTableColumn(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])->defaultSort('position');
    }

    protected static function getAttributeDataFormComponent(): Component
    {
        return Attributes::make()->statePath('attribute_data');
    }

    public static function getNameTableColumn(): Tables\Columns\Column
    {
        return TranslatedTextColumn::make('attribute_data.name')
            ->attributeData()
            ->limitedTooltip()
            ->limit(50)
            ->label(__('lunarpanel::product.table.name.label'));
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBanners::route('/'),
        ];
    }
}

Except this I have also Pages: ListBanners and ManageBanners - default created from filament doc

glennjacobs commented 4 months ago

I can't spot what is wrong with your code. However, we also have a "banners" resource on one of our sites and changing the path to "hub" didn't affect it's ability to work.

Below is my resource class in case it can help you at all

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BannerResource\Pages;
use App\Filament\Resources\BannerResource\RelationManagers\CollectionRelationManager;
use App\Filament\Resources\BannerResource\RelationManagers\ProductRelationManager;
use App\Models\Banner;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Lunar\Admin\Support\Resources\BaseResource;

class BannerResource extends BaseResource
{
    protected static ?string $permission = 'catalog:manage-products';

    protected static ?string $model = Banner::class;

    protected static ?string $slug = 'banners';

    public static function getLabel(): string
    {
        return 'Banner';
    }

    public static function getPluralLabel(): string
    {
        return 'Banners';
    }

    public static function getNavigationIcon(): ?string
    {
        return 'lucide-layout-panel-top';
    }

    public static function getNavigationGroup(): ?string
    {
        return 'CMS';
    }

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::count();
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\Section::make()->schema([
                FileUpload::make('image')
                    ->disk('s3')
                    ->directory('banners')
                    ->visibility('public'),
                TextInput::make('button_url')->helperText(
                    'Add an optional URL to render a button on the banner.'
                ),
            ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            ImageColumn::make('image')->disk('s3'),
            TextColumn::make('collections_count')
                ->label('No. Collections')
                ->counts('collections'),
            TextColumn::make('products_count')
                ->label('No. Products')
                ->counts('products'),

            TextColumn::make('button_url'),
        ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListBanners::route('/'),
            'create' => Pages\CreateBanner::route('/create'),
            'edit' => Pages\EditBanner::route('/{record}/edit'),
        ];
    }

    public static function getRelations(): array
    {
        return [
            RelationGroup::make('Resources', [
                CollectionRelationManager::class,
                ProductRelationManager::class,
            ]),
        ];
    }
}
morawcik commented 4 months ago

That's weird...

I log routes ($routeBaseName) inside Filament\Resources\Resource in getUrl method and I can see that it uses lunar for each resource

[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.activities  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.attribute-groups  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.brands  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.channels  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.collection-groups  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.currencies  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.customer-groups  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.customers  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.discounts  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.languages  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.orders  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.product-options  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.products  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.product-types  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.staff  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.tags  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.tax-classes  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.tax-zones  
[2024-05-20 22:28:24] local.INFO: filament.lunar.resources.banners  

One more time to be sure - that's my AppServiceProvider register method:

public function register(): void
{
    LunarPanel::panel(function ($panel) {
        return $panel->path('hub')
            ->resources([
                BannerResource::class,
            ]);
    })->register();
}
glennjacobs commented 4 months ago

Yeah, service provider looks the same as mine.

Have you tried clearing caches, etc? e.g. php artisan route:clear

morawcik commented 4 months ago

I did - route, cache and even config.

Ok, don't worry about that - that's nothing what I couldn't live without 😄

glennjacobs commented 4 months ago

This is what I have

image

The panel's ID is "lunar", so I believe that will always remain regardless of the path choice.

glennjacobs commented 4 months ago

I'm going to close, as I don't think this is a Lunar bug. But you're free to continue the conversation here.

morawcik commented 4 months ago

Hi, I want to let you know that after composer update (dunno if it was after Lunar or Filament update - if filament is not version locked) it works fine :)