DocumentaçãoDeveloper guideExemplo completo
Hello Notes, passo a passo
Um pequeno aplicativo que passa por todas as partes: um manifesto, uma tabela, rotas com login, duas páginas Vue, um valor de configuração, suas próprias permissões e seu próprio menu. Ele é instalado a partir de um pacote assinado.
1 min de leitura
1. O manifesto
<?php
return [
'slug' => 'hello',
'name' => 'Hello Notes',
'version' => '1.0.0',
'requires' => ['optirapos' => '>=1.0.0'],
'summary' => 'Short notes for your team, the developer guide example',
'category' => 'Productivity',
'author' => 'Your company',
'depends' => [],
'abilities' => ['key' => 'hello', 'label' => 'Hello Notes', 'abilities' => [
['key' => 'hello.view', 'label' => 'View'],
['key' => 'hello.manage', 'label' => 'Manage'],
]],
'menu' => [[
'key' => 'hello', 'label' => 'Hello Notes', 'icon' => 'M4 5h16M4 12h16M4 19h10',
'ability' => 'hello.view', 'match' => ['/hello'],
'groups' => [
['label' => 'Notes', 'items' => [['label' => 'Notes', 'href' => '/hello']]],
['label' => 'Configuration', 'items' => [['label' => 'Settings', 'href' => '/hello/settings']]],
],
'sequence' => 50,
]],
'segments' => ['hello' => ['hello.view']],
'assets' => [],
];2. A tabela
database/migrations/2027_04_01_000100_hello_create_notes.php. O nome traz hello_ depois da hora, como Tabelas e modelos exige:
Schema::create('hello_notes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('company_id')->index();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('body', 500);
$table->timestamps();
});O modelo Models/HelloNote.php define $table = 'hello_notes' e $fillable = ['company_id', 'user_id', 'body'].
3. Rotas
Route::get('/hello', [HelloController::class, 'index'])->name('hello.index');
Route::post('/hello', [HelloController::class, 'store'])->name('hello.store');
Route::get('/hello/settings', [HelloController::class, 'settings'])->name('hello.settings');
Route::put('/hello/settings', [HelloController::class, 'saveSettings'])->name('hello.settings.save');4. O controlador
Cada ação verifica a permissão certa, lê só as empresas do usuário e renderiza uma página hello:::
public function index(Request $request)
{
$this->allow($request, 'hello.view'); // isAdmin() || hasAbility(...), else 403
return Inertia::render('hello::Index', [
'greeting' => ConfigParameter::get('hello.greeting', 'Hello from your first add-on'),
'notes' => HelloNote::query()->whereIn('company_id', CompanyContext::activeIds())
->latest('id')->limit(50)->get(['id', 'body', 'created_at']),
'canManage' => $request->user()->isAdmin() || $request->user()->hasAbility('hello.manage'),
]);
}store valida body, salva em CompanyContext::defaultCompanyId() e redireciona com UiText::t('Note saved.'). settings e saveSettings leem e gravam hello.greeting com ConfigParameter.
5. As páginas
dist/addon.js é escrito à mão, então o exemplo não precisa de nenhuma etapa de build. Ele pega o Vue, o Inertia e o layout do hospedeiro e registra duas páginas:
(() => {
const { h } = window.OptiraVendor.vue;
const { useForm } = window.OptiraVendor.inertia;
const Layout = window.OptiraVendor.ui.OptiraLayout;
const Index = { props: { greeting: String, notes: Array, canManage: Boolean }, setup(props) {
const form = useForm({ body: '' });
return () => h(Layout, { title: 'Notes' }, () => h('div', { class: 'p-4' }, [
h('p', props.greeting),
/* a form posting form.post('/hello'), then the list of props.notes */
]));
} };
const Settings = { /* one field, saved with form.put('/hello/settings') */ };
Object.assign(window.OptiraAddons.pages, { 'hello::Index': Index, 'hello::Settings': Settings });
})();Os arquivos completos vêm ao lado deste guia, em Hello/.
6. Empacotar, assinar, instalar
Siga Empacotar e assinar e Instalar e ativar. No sistema de teste em que este guia foi verificado, o pacote foi gerado com 6 arquivos e instalado por Enviar pacote, e as duas páginas abriram.
Verificado em uma instalação limpa do pacote da versão OptiraPOS 1.0: o pacote assinado do Hello Notes é enviado, ativado pela linha de comando, e as duas páginas abrem.