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

Show Blade View

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

Display Page

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

Show Notification

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

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

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

JSON Response

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: []);
}
}