Advanced

Controllers

MoonShine allows you to work in a familiar manner using controllers

We provide you with our basic controller, which helps you conveniently work with the UI and render your views with MoonShine layout

Useful for displaying your complex solutions or writing additional handlers

Generate controller

php artisan moonshine:controller

Show blade view

namespace App\MoonShine\Controllers;
 
use MoonShine\MoonShineRequest;
use MoonShine\Http\Controllers\MoonshineController;
use Illuminate\Contracts\View\View;
 
final class CustomViewController extends MoonshineController
{
public function __invoke(MoonShineRequest $request): View
{
return $this
->view('path_to_blade', ['param' => 'value'])
//->setLayout('custom_layout')
->render();
}
}

Display page

namespace App\MoonShine\Controllers;
 
use MoonShine\MoonShineRequest;
use MoonShine\Http\Controllers\MoonshineController;
use MoonShine\Pages\Page;
 
final class CustomViewController extends MoonshineController
{
public function __invoke(MoonShineRequest $request): Page
{
return MyPage::make();
}
}

Show notification

namespace App\MoonShine\Controllers;
 
use MoonShine\MoonShineRequest;
use MoonShine\Http\Controllers\MoonshineController;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonshineController
{
public function __invoke(MoonShineRequest $request): Response
{
$this->toast('Hello world');
 
return back();
}
}

Send notification

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

Access a page or resource

namespace App\MoonShine\Controllers;
 
use MoonShine\MoonShineRequest;
use MoonShine\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\MoonShineRequest;
use MoonShine\Http\Controllers\MoonshineController;
use Symfony\Component\HttpFoundation\Response;
 
final class CustomViewController extends MoonshineController
{
public function __invoke(MoonShineRequest $request): Response
{
return $this->json(message: 'Message', data: [], redirect: null);
}
}