Install via Composer
Add to composer.json:
"require": {
"ais/userbundle": "dev-master"
}
Run:
composer update
Register Bundle
Add to app/AppKernel.php:
new Ais\UserBundle\AisUserBundle(),
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle()
Configure Routing
Add to app/config/routing.yml:
ais_users:
type: rest
prefix: /api
resource: "@AisUserBundle/Resources/config/routes.yml"
Verify API Docs
Access /api/doc (e.g., http://localhost/web/app_dev.php/api/doc) to explore endpoints.
POST /api/users/register with JSON payload:
{
"username": "testuser",
"email": "test@example.com",
"password": "secure123"
}
FOSOAuthServerBundle) for authentication.User CRUD via REST
POST /api/users (with FOSRestBundle serialization).GET /api/users/{id} (JMS Serializer handles output).PUT /api/users/{id} (patchable via WillDurand/RestExtraBundle).DELETE /api/users/{id}.Authentication Flow
FOSOAuthServerBundle for token-based auth (e.g., /oauth/v2/token).Authorization: Bearer <token>.Event-Driven Extensions
AisUserEvents (e.g., USER_REGISTERED) via Symfony’s event dispatcher:
$dispatcher->addListener('AisUserEvents::USER_REGISTERED', function ($event) {
// Custom logic (e.g., send welcome email)
});
Custom User Fields
Extend the User entity (located in AisUserBundle/Entity/User.php):
namespace AppBundle\Entity;
use Ais\UserBundle\Entity\User as BaseUser;
class User extends BaseUser {
/**
* @ORM\Column(type="string", nullable=true)
*/
private $customField;
}
Update config.yml to override the entity:
ais_user:
user_class: AppBundle\Entity\User
API Documentation
Leverage NelmioApiDocBundle to auto-generate Swagger docs. Annotate controllers:
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* @ApiDoc(
* resource=true,
* description="User registration"
* )
*/
class UserController extends FOSRestController
Testing
Use LiipFunctionalTestBundle for API tests:
$client = static::createClient();
$client->request('POST', '/api/users/register', [
'json' => ['username' => 'test', 'password' => 'pass']
]);
Deprecated Symfony 2.7
Doctrine\ORM\EntityManager directly or wrap in a repository.Missing Laravel-Specific Features
Auth facade or HasApiTokens traits.FOSOAuthServerBundle.Route Conflicts
ais_users routes may clash with existing /api/users endpoints.routes.yml or use _format suffixes:
ais_users:
type: rest
prefix: /api/v1
resource: "@AisUserBundle/Resources/config/routes.yml"
Enable API Debugging
Add to config.yml:
nelmio_api_doc:
areas: { path_patterns: ['^/api'] }
sandbox:
method: POST
headers:
Authorization: "Bearer {token}"
Check Event Dispatching
Verify events fire in app_dev.php:
$dispatcher = $this->get('event_dispatcher');
$dispatcher->addListener('AisUserEvents::USER_REGISTERED', function ($event) {
error_log('Event triggered: ' . print_r($event->getUser(), true));
});
Serializer Issues
php app/console jms:serializer:update-cache-proxies
Custom Controllers
Override the default UserController by configuring:
ais_user:
controller:
user: AppBundle\Controller\UserController
Validation Rules Extend validation via Symfony’s constraints:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank()
* @Assert\Length(min=6)
*/
private $password;
Email Templates
Override Twig templates in app/Resources/AisUserBundle/views/ (e.g., Registration/email.txt.twig).
How can I help you explore Laravel packages today?