-
Notifications
You must be signed in to change notification settings - Fork 58
Caching the Sidebar
When dealing with a lot of menu items, authorization checks, badges filled by values from the database,... the sidebar can become a little bit heavier. By utilising caching, we can make this process a bit lighter.
If you have a static sidebar, which is the same for everybody, you can change the sidebar.cache.method
option to static
. If the sidebar can be different for every logged in user, you should set the config setting to user-based
.
The class should implement Maatwebsite\Sidebar\ShouldCache
and use the Maatwebsite\Sidebar\Traits\CacheableTrait
trait.
use Maatwebsite\Sidebar\Menu;
use Maatwebsite\Sidebar\Sidebar;
use Maatwebsite\Sidebar\ShouldCache;
use Illuminate\Contracts\Auth\Guard;
use Maatwebsite\Sidebar\Traits\CacheableTrait;
class AdministrationSidebar implements Sidebar, ShouldCache
{
use CacheableTrait;
/**
* @var User
*/
protected $user;
/**
* @var
*/
protected $guard;
/**
* @var
*/
protected $menu;
/**
* @param Menu $menu
* @param Guard $guard
*/
public function __construct(Menu $menu, Guard $guard)
{
$this->menu = $menu;
$this->guard = $guard;
$this->user = $guard->user();
}
/**
* Build the sidebar
*/
public function build()
{
// Build menu
}
/**
* @return Menu
*/
public function getMenu()
{
return $this->menu;
}
}
You can use the Maatwebsite\Sidebar\Domain\Events\FlushesSidebarCache
event handler in your own application to tell the package when it should flush the cache. The event class itself should implement Maatwebsite\Sidebar\Domain\Events\ShouldFlushCache