PHP code example of morphcms / command-palette-module
1. Go to this page and download the library: Download morphcms/command-palette-module library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
morphcms / command-palette-module example snippets
CommandSearch::class => [
// Register your listeners here.
],
use Modules\CommandPalette\ActionTypes\NavigateAction;
use Modules\CommandPalette\ActionTypes\ToastAction;
use Modules\CommandPalette\DTO\SearchResult;
use Modules\CommandPalette\Enums\CommandIcon;
use Modules\CommandPalette\Listeners\CollectionSearchListener;
class GeneralCommandsSearchListener extends CollectionSearchListener
{
public function items(): array
{
return [
SearchResult::make('Greet') // The command title that will also be searched
->group('Admin') // Just a name to nicely group results
->icon(CommandIcon::LightningBolt) // Uses Hero Icons package
->description('Says hello back to you.') // A summary of what this command or this result does.
->action(ToastAction::make()->message('Hello, '.auth()->user()->name.'!')), // Action is catched on the frontend, this should send a payload
];
}
}
namespace App\Listeners;
use Illuminate\Support\Collection;
use Modules\Blog\Models\Post;
use Modules\CommandPalette\ActionTypes\NavigateAction;
use Modules\CommandPalette\DTO\SearchResult;
use Modules\CommandPalette\Enums\CommandIcon;
use Modules\CommandPalette\Events\CommandSearch;
use Modules\CommandPalette\Listeners\CommandSearchListener;
class GeneralCommandsSearchListener extends CommandSearchListener
{
protected function search(CommandSearch $event): Collection|null
{
return Model::search($event->query)
->limit($event->limit)
->get()
->map(fn (Model $model) =>
SearchResult::make(
title: $model->title,
type: 'resource_name',
)->group('Resource Name')
->icon(CommandIcon::Sparkles)
->action(NavigateAction::make(['id' => $model->id]))
);
}
}
namespace App\Listeners\Commands;
use Illuminate\Support\Collection;
use Modules\Blog\Models\Post;
use Modules\Shop\Enums\ProductStatus;
use Modules\CommandPalette\ActionTypes\NavigateAction;
use Modules\CommandPalette\DTO\SearchResult;
use Modules\CommandPalette\Enums\CommandIcon;
use Modules\CommandPalette\Events\CommandSearch;
use Modules\CommandPalette\Listeners\CommandSearchListener;
class BlogCommandsSearchListener extends CommandSearchListener
{
// This implementation uses Scout for search
protected function search(CommandSearch $event): Collection|null
{
return Post::search($event->query)
->query(fn ($q) => $q->where('status', ProductStatus::Published->value))
->get()
->map(fn (Post $post) => SearchResult::make(
title: $post->title,
type: 'post',
)->group('Blog')
->icon(CommandIcon::DocumentText)
->action(NavigateAction::make(['slug' => $post->slug]))
);
}
}
namespace App\Listeners\Commands;
use Laravel\Nova\Nova;use Modules\CommandPalette\ActionTypes\NavigateAction;
use Modules\CommandPalette\ActionTypes\ToastAction;
use Modules\CommandPalette\DTO\SearchResult;
use Modules\CommandPalette\Enums\CommandIcon;
use Modules\CommandPalette\Listeners\CollectionSearchListener;
class AdminCommandsSearchListener extends CollectionSearchListener
{
public function authorize(): bool
{
return auth()->check() && auth()->user()->is_admin;
}
public function items(): array
{
return [
SearchResult::make('Create Product')
->group('Admin')
->icon(CommandIcon::LightningBolt)
->action(
NavigateAction::make([
'url' => Nova::url('/resources/products/new'),
])),
];
}
}
namespace Modules\CommandPalette\ActionTypes;
use Modules\CommandPalette\DTO\CommandAction;
class MyCommandAction extends CommandAction
{
public function __construct(array $meta = [])
{
parent::__construct('my-type', $meta);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.