Symlink the Bundle
Ensure the bundle is symlinked to /src/Awaresoft in your Laravel project (via utils/prepare_vendors or manual symlinking).
Remove any Composer-installed version and update autoload_psr4.php to reflect the local path.
Register the Bundle
In config/app.php, add the bundle to the extra.bundles array:
Awaresoft\SonataCoreBundle\SonataCoreBundle::class,
Clear Cache Run:
php artisan cache:clear
php artisan config:clear
First Use Case: Basic Admin Interface Extend the bundle’s admin generator to create a CRUD interface for a model:
php artisan sonata:admin:generate --model=User --bundle=AcmeDemoBundle
Then register the admin in services.yml (Symfony config) or via Laravel’s service provider.
Custom Admin Classes
Override default admin behavior by extending SonataAdminService:
namespace Acme\DemoBundle\Admin;
use Awaresoft\SonataCoreBundle\Admin\AdminService;
class UserAdmin extends AdminService
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id')
->add('email')
->add('roles');
}
}
Dynamic Route Configuration
Use the bundle’s router to define admin routes in routing.yml:
acme_demo_admin:
resource: '@AcmeDemoBundle/Resources/config/routing/sonata_admin.xml'
prefix: /admin
Form Customization
Extend forms via configureFormFields:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text')
->add('isActive', 'checkbox', [
'required' => false,
]);
}
Integration with Laravel Services Inject Laravel services (e.g., repositories) into admin classes via constructor:
public function __construct(UserRepository $userRepo)
{
$this->userRepo = $userRepo;
parent::__construct($id, $class, $baseControllerName);
}
Event Listeners
Hook into lifecycle events (e.g., prePersist, postRemove) in your admin class:
public function prePersist($object)
{
$object->setCreatedAt(now());
}
Symfony vs. Laravel Compatibility
symfony/console) for CLI tools.EventDispatcher with Laravel’s events directly; wrap or proxy calls.Cache Invalidation After modifying admin classes or templates, clear both Laravel and Symfony caches:
php artisan cache:clear
php artisan config:clear
php app/console cache:clear --env=prod
Route Conflicts
Ensure admin routes (/admin/*) don’t clash with Laravel’s default routes. Use route prefixes or middleware to isolate them.
Template Overrides
Override Sonata templates in resources/views/bundles/sonataadmin/ (Laravel) or templates/ (Symfony). Prioritize:
/templates/AwaresoftSonataCoreBundle:Default:base_layout.html.twig
Database Schema Migrations
The bundle doesn’t handle migrations. Use Laravel’s migrate or Symfony’s doctrine:migrations separately.
SonataAdminService to log actions:
public function postPersist($object)
{
\Log::info("Created {$object->getClass()} with ID {$object->getId()}");
}
php artisan route:list | grep admin
Custom Controllers
Override the base controller (SonataAdminController) for custom logic:
namespace Acme\DemoBundle\Controller;
use Awaresoft\SonataCoreBundle\Controller\SonataAdminController;
class CustomAdminController extends SonataAdminController
{
public function customAction()
{
return $this->render('AcmeDemoBundle:Admin:custom.html.twig');
}
}
Dynamic Permissions Use the bundle’s security system to restrict access:
# config/security.yml
access_control:
- { path: ^/admin/, role: ROLE_ADMIN }
API Integration
Expose admin data via API by extending controllers or using Laravel’s apiResource alongside Sonata routes.
Localization
Override translation files in translations/AwaresoftSonataCoreBundle.en.yml (Symfony) or resources/lang/ (Laravel).
Testing
Use Symfony’s WebTestCase or Laravel’s HttpTests to test admin routes:
public function testAdminList()
{
$client = static::createClient();
$client->request('GET', '/admin/user');
$this->assertResponseStatusCodeSuccess($client);
}
How can I help you explore Laravel packages today?