1. Go to this page and download the library: Download tcds-io/laravel-prince 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/ */
// All resources in this builder cap at 50
ModelResourceBuilder::create(maxLimit: 50)
->resource(Invoice::class)
->resource(Product::class, maxLimit: 10) // this one caps at 10
->routes();
// Check permission against the authenticated user
fn(RequestContext $ctx) => in_array($ctx->permission, Auth::user()?->permissions ?? [])
// Inject a service — no RequestContext needed
fn(AuthService $auth) => $auth->isAdmin()
// Use both
fn(RequestContext $ctx, AuthService $auth) => $auth->can($ctx->permission)
// Flat allow/deny (e.g. for public read-only endpoints)
fn() => false
// Read-only resource: anyone can list/get, create/update/delete are not registered
ModelResourceBuilder::create()
->authorizer(fn(RequestContext $ctx) => in_array($ctx->permission, $user->permissions))
->resource(
model: Product::class,
permissions: [
'read' => 'public',
],
)
->routes();
use Tcds\Io\Prince\AuthorizerContext;
use Tcds\Io\Prince\ResourceAction;
ModelResourceBuilder::create()
->authorizer(fn(AuthorizerContext $ctx) => in_array($ctx->permission, $user->permissions))
->resource(
model: Invoice::class,
permissions: [
'read' => 'invoices:read',
'create' => 'invoices:write',
'update' => 'invoices:write',
'delete' => 'invoices:delete',
],
actions: [
// Collection-level — POST /invoices/import
ResourceAction::post(
path: '/import',
action: ImportInvoicesAction::class,
permission: 'invoices:write',
),
// Item-level — POST /invoices/{id}/send
// The matching Invoice is resolved and injected; returns 404 if not found.
ResourceAction::post(
path: '/{id}/send',
action: SendInvoiceAction::class,
permission: 'invoices:send',
),
// GET /invoices/{id}/pdf
ResourceAction::get(
path: '/{id}/pdf',
action: InvoicePdfController::class,
permission: 'invoices:read',
),
],
)
->routes();
use Tcds\Io\Prince\Events\ResourceCreated;
use Tcds\Io\Prince\Events\ResourceCreating;
// Side effect — send notification after create
Event::listen(ResourceCreated::class, function (ResourceCreated $event): void {
if ($event->model instanceof Invoice) {
Notification::send($event->model->user, new InvoiceCreatedNotification($event->model));
}
});
// Data mutation — slugify title before save
Event::listen(ResourceCreating::class, function (ResourceCreating $event): void {
if (isset($event->data['title'])) {
$event->data['slug'] = Str::slug($event->data['title']);
}
});
use Tcds\Io\Prince\Events\MutableDataEvent;
class InvoiceCreating implements MutableDataEvent
{
public function __construct(
public readonly string $modelName,
public array $data,
) {}
}
ModelResourceBuilder::create()
->authorizer(fn(RequestContext $ctx) => in_array($ctx->permission, $user->permissions))
->resource(model: Invoice::class, segment: 'bills')
->routes();
// Routes registered at /bills/... — table name remains invoices in meta/schema
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.