carlespibernat/admin-generator-bundle
Installation Add the bundle via Composer:
composer require carlespibernat/admin-generator-bundle
Register the bundle in config/app.php under providers:
Carlespibernat\AdminGeneratorBundle\CarlespibernatAdminGeneratorBundle::class,
Configuration Publish the default config:
php artisan admin-generator:install
Update config/admin_generator.php to match your needs (e.g., default_namespace, model_namespace).
First Use Case
Generate a CRUD for an existing model (e.g., User):
php artisan admin-generator:generate User
This creates:
UserAdminController).index.html.twig, edit.html.twig, etc.) in resources/views/admin/user/.routes/admin.php.Access the Admin Panel
Visit /admin/user (or your configured route prefix). Authenticate via your existing auth system (e.g., Symfony Guard or Laravel’s built-in auth).
Model-Driven Generation
php artisan admin-generator:generate ModelName to scaffold a full CRUD.--force:
php artisan admin-generator:generate User --force
php artisan admin-generator:generate User --only=controller
Customization
vendor/carlespibernat/admin-generator-bundle/resources/views/ to resources/views/admin/ to customize them globally.resources/views/admin/{entity_name}/ (e.g., resources/views/admin/user/edit.html.twig).namespace App\Admin;
use Carlespibernat\AdminGeneratorBundle\Admin\AbstractAdminController;
class UserAdminController extends AbstractAdminController
{
public function customize()
{
$this->addField('custom_field', 'text');
}
}
Integration with Laravel
routes/admin.php:
Route::group(['middleware' => ['auth']], function () {
$this->loadRoutesFrom(__DIR__.'/admin_routes.php');
});
authorize() method or use Laravel’s gates/policies.FormRequest:
use App\Http\Requests\StoreUserRequest;
public function store(StoreUserRequest $request)
{
// Custom validation/logic
}
Dynamic Fields
customize() method:
public function customize()
{
$this->addField('active', 'boolean', [
'label' => 'Is Active',
'editable' => true,
]);
}
$this->addAssociation('roles', 'belongsToMany', 'Role');
API Endpoints
--api:
php artisan admin-generator:generate User --api
Resource classes or custom JSON logic.webpack.mix.js compiles assets for the admin namespace if using custom JS/CSS.lang/ directory) for labels/buttons. Override the bundle’s translations in config/admin_generator.php:
'translations' => [
'default_locale' => 'en',
'paths' => [__DIR__.'/../resources/lang'],
],
php artisan migrate
$this->get('/admin/user')->assertStatus(200);
Namespace Conflicts
default_namespace in config/admin_generator.php matches your Laravel app’s namespace (e.g., App\Admin).Route Caching
php artisan route:clear
config/admin_generator.php:
'route_cache' => false,
Twig vs. Blade
tightenco/jigsaw or manually convert Twig templates.protected $templateEngine = 'blade';
Model Not Found
App\Models\User). The generator skips non-existent models silently.CSRF Token Mismatch
Asset Paths
asset() helper or absolute URLs to avoid 404s.Generator Logs
php artisan admin-generator:generate User --verbose
storage/logs/laravel.log for errors.Template Debugging
config/admin_generator.php:
'twig' => [
'debug' => env('APP_DEBUG', true),
],
dump() function in templates for debugging.Route Debugging
php artisan route:list --path=/admin
php artisan route:test.Database Issues
$table property. Use:
php artisan schema:dump
to inspect the schema.Partial Regeneration
php artisan admin-generator:generate User --only=views
Multi-Tenant Support
public function customize()
{
$this->addFilter('tenant_id', 'number', [
'label' => 'Tenant ID',
'value' => auth()->user()->tenant_id,
]);
}
Soft Deletes
public function customize()
{
$this->addField('deleted_at', 'datetime', [
'editable' => false,
'label' => 'Deleted At',
]);
}
Bulk Actions
customize() method:
public function customize()
{
$this->addBulkAction('delete', 'Delete Selected', 'deleteBulk');
}
public function deleteBulk(Request $request)
{
$this->model->whereIn('id', $request->input('ids'))->delete();
return redirect()->back()->with('success', 'Items deleted');
}
Search/Filter Optimization
public function getListQuery()
{
return $this->model->where('active', true);
}
Localization
translations.php file in the entity’s directory (e.g., resources/admin/user/translations.php):
return [
'buttons' => [
'save' => 'Guardar Cambios',
],
];
Performance
getList() method:
public function getList()
{
return $this->model->with('roles')->get();
}
return $this->model->paginate(20);
How can I help you explore Laravel packages today?