导航

用户菜单

简介

用户菜单位于管理界面右上角,可完全自定义。

每个菜单项都由一个 Action 表示,并可按照相同的方式进行自定义。要注册新菜单项,你可以将这些操作传递给配置userMenuItems() 方法:

use App\Filament\Pages\Settings;
use Filament\Actions\Action;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenuItems([
            Action::make('settings')
                ->url(fn (): string => Settings::getUrl())
                ->icon('heroicon-o-cog-6-tooth'),
            // ...
        ]);
}
User menu with custom menu item

Grouping user menu items

By default, all user menu items are rendered in a single list. If you want to separate them into distinct groups, you can pass an array of arrays to the userMenuItems() method. Each array is rendered as its own group, separated by a divider:

use App\Filament\Pages\Billing;
use App\Filament\Pages\Settings;
use Filament\Actions\Action;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenuItems([
            [
                Action::make('settings')
                    ->url(fn (): string => Settings::getUrl())
                    ->icon('heroicon-o-cog-6-tooth'),
                Action::make('billing')
                    ->url(fn (): string => Billing::getUrl())
                    ->icon('heroicon-o-banknotes'),
            ],
            [
                Action::make('documentation')
                    ->url('https://filamentphp.com/docs')
                    ->icon('heroicon-o-book-open'),
            ],
        ]);
}
User menu items split into separate groups

NOTE

The logout item is added to the last group by default. To place it yourself, register it explicitly in any group using the logout array key. Since its default sort() puts it at the end of its group, adjust the sort if you want it elsewhere within the group:

->userMenuItems([
    // ...
    [
        'logout' => fn (Action $action): Action => $action->sort(2),
    ],
])

Moving the user menu to the sidebar

By default, the user menu is positioned in the topbar. If the topbar is disabled, it is added to the sidebar.

You can choose to always move it to the sidebar by passing a position argument to the userMenu() method in the configuration:

use Filament\Enums\UserMenuPosition;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenu(position: UserMenuPosition::Sidebar);
}
User menu moved to the sidebar

自定义个人资料链接

要自定义用户菜单开头的用户个人资料链接,请使用 profile 数组键注册一个新项目,并传递一个自定义操作对象的函数:

use Filament\Actions\Action;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenuItems([
            'profile' => fn (Action $action) => $action->label('Edit profile'),
            // ...
        ]);
}

有关创建个人资料页面的更多信息,请参阅身份验证功能文档

自定义退出链接

要自定义用户菜单末尾的用户退出链接,请使用 logout 数组键注册一个新项目,并传递一个自定义操作对象的函数:

use Filament\Actions\Action;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenuItems([
            'logout' => fn (Action $action) => $action->label('Log out'),
            // ...
        ]);
}

条件性地隐藏用户菜单项

你也可以使用 visible()hidden() 方法,传入要检查的条件,条件性地隐藏用户菜单项。传入一个函数会将条件判断推迟到菜单实际渲染时:

use App\Models\Payment;
use Filament\Actions\Action;

Action::make('payments')
    ->visible(fn (): bool => auth()->user()->can('viewAny', Payment::class))
    // or
    ->hidden(fn (): bool => ! auth()->user()->can('viewAny', Payment::class))

从用户菜单项发送 POST HTTP 请求

你可以通过将 URL 传递给 url() 方法,以及使用 postToUrl(),从用户菜单项发送 POST HTTP 请求:

use Filament\Actions\Action;

Action::make('lockSession')
    ->url(fn (): string => route('lock-session'))
    ->postToUrl()

禁用用户菜单

你也可以通过将 false 传递给 userMenu() 方法完全禁用用户菜单:

use Filament\Panel;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->userMenu(false);
}
Edit on GitHub

Still need help? Join our Discord community or open a GitHub discussion

Previous
自定义页面