Advanced

Контроллеры

MoonShine позволяет работать привычным образом, используя контроллеры

Мы предоставляем вам наш базовый контроллер, который помогает удобно работать с UI и отображать ваши представления с макетом MoonShine

Это полезно для отображения ваших сложных решений или написания дополнительных обработчиков

Генерация контроллера

php artisan moonshine:controller
php artisan moonshine:controller

Отображение blade-представления

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();
}
}
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();
}
}

Отображение страницы

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();
}
}
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();
}
}

Показать уведомление

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();
}
}
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();
}
}

Отправить уведомление

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();
}
}
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();
}
}

Доступ к странице или ресурсу

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();
}
}
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-ответ

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);
}
}
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);
}
}