Advanced

Controllers

MoonShine allows you to work in a familiar way using controllers.

We provide you with our base controller, which helps to conveniently interact with the UI and display your views with the MoonShine layout.

This is useful for showcasing your complex solutions or writing additional handlers.

Inheriting from MoonshineController is not mandatory; we merely provide convenient ready-made methods.

Generate Controller

php artisan moonshine:controller

You can learn about all supported options in the section Commands.

Show Blade View

 namespaces
namespace App\MoonShine\Controllers;
 
use MoonShine\Contracts\Core\PageContract;
use MoonShine\Laravel\Http\Controllers\MoonShineController;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(): PageContract
{
return $this->view(
'path_to_blade',
['param' => 'value']
);
}
}

Display Page

 namespaces
namespace App\MoonShine\Controllers;
 
use App\MoonShine\Pages\MyPage;
use MoonShine\Laravel\Http\Controllers\MoonShineController;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(MyPage $page): MyPage
{
return $page->loaded();
}
}

Show Notification

The toast() method triggers a standard toast notification of the admin panel.

 namespaces
namespace App\MoonShine\Controllers;
 
use MoonShine\Laravel\Http\Controllers\MoonShineController;
use MoonShine\Support\Enums\ToastType;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(): Response
{
$this->toast('Hello world', ToastType::SUCCESS);
 
return back();
}
}

Send Notification

 namespaces
namespace App\MoonShine\Controllers;
 
use MoonShine\Laravel\Http\Controllers\MoonShineController;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(): Response
{
$this->notification('Message');
 
return back();
}
}

Access a Page or Resource

 namespaces
namespace App\MoonShine\Controllers;
 
use MoonShine\Laravel\Http\Controllers\MoonShineController;
use MoonShine\Laravel\MoonShineRequest;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(MoonShineRequest $request)
{
// $request->getPage();
// $request->getResource();
}
}

JSON Response

 namespaces
namespace App\MoonShine\Controllers;
 
use MoonShine\Laravel\Http\Controllers\MoonShineController;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonShineController
{
public function __invoke(): Response
{
return $this->json(message: 'Message', data: []);
}
}