## Getting Started
### Minimal Setup
1. **Installation**:
- Add the package to `composer.json` under `require`:
```json
"ais/mahasiswaprofilebundle": "dev-master"
```
- Run `composer update` in your project root.
2. **Register Bundle**:
Add the bundle to `AppKernel.php` under `registerBundles()`:
```php
new Ais\MahasiswaProfileBundle\AisMahasiswaProfileBundle(),
Ensure dependencies (NelmioApiDocBundle, FOSRestBundle, JMSSerializerBundle) are also included.
Routing:
Import routes in app/config/routing.yml:
ais_mahasiswaprofiles:
type: rest
prefix: /api
resource: "@AisMahasiswaProfileBundle/Resources/config/routes.yml"
Access API Docs:
Navigate to /api/doc (e.g., http://localhost/web/app_dev.php/api/doc) to explore endpoints.
GET /api/mahasiswaprofiles/{id}) to retrieve a student profile.NelmioApiDocBundle to inspect available routes and request/response formats.CRUD Operations:
POST /api/mahasiswaprofiles for creation, PUT /api/mahasiswaprofiles/{id} for updates).@Assert\NotBlank in entities).Data Serialization:
JMSSerializerBundle (e.g., override serializer.yml for specific fields).# app/config/config.yml
jms_serializer:
metadata:
directories:
AisMahasiswaProfileBundle:
namespace_prefix: "Ais\\MahasiswaProfileBundle\\Serializer"
path: "%kernel.root_dir%/serializer"
Integration with Doctrine:
MahasiswaProfile entity (e.g., src/Ais/MahasiswaProfileBundle/Entity/MahasiswaProfile.php) to add custom fields or behaviors.namespace Ais\MahasiswaProfileBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class MahasiswaProfile {
// ...
/**
* @ORM\Column(type="string", length=255)
*/
private $customField;
}
API Documentation:
NelmioApiDocBundle annotations to document endpoints (e.g., @SWG\Tag(name="Mahasiswa")).use Nelmio\ApiDocBundle\Annotation\ApiDoc;
/**
* @ApiDoc(
* resource=true,
* description="Get a student profile"
* )
*/
Event Listeners:
prePersist, preUpdate) to modify data before saving.// src/Ais/MahasiswaProfileBundle/EventListener/MahasiswaProfileListener.php
namespace Ais\MahasiswaProfileBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
class MahasiswaProfileListener {
public function prePersist(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if ($entity instanceof MahasiswaProfile) {
$entity->setCreatedAt(new \DateTime());
}
}
}
services.yml:
services:
ais_mahasiswaprofile.listener:
class: Ais\MahasiswaProfileBundle\EventListener\MahasiswaProfileListener
tags:
- { name: doctrine.event_listener, event: prePersist }
Custom Controllers:
src/Ais/MahasiswaProfileBundle/Controller/MahasiswaProfileController.php) to add business logic.namespace Ais\MahasiswaProfileBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
class MahasiswaProfileController extends FOSRestController {
public function customAction(Request $request) {
// Custom logic
return $this->handleView($this->view(['data' => 'custom']));
}
}
Form Handling:
MahasiswaProfileType).// src/Ais/MahasiswaProfileBundle/Form/MahasiswaProfileType.php
namespace Ais\MahasiswaProfileBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MahasiswaProfileType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('name', 'text');
$builder->add('email', 'email');
}
}
Symfony 2.7 Legacy:
Missing Type Safety:
return MahasiswaProfile instead of return array). Add type hints manually for better IDE support.Undocumented Dependencies:
NelmioApiDocBundle, FOSRestBundle, and JMSSerializerBundle but doesn’t document their versions explicitly. Pin versions in composer.json to avoid conflicts:
"nelmio/api-doc-bundle": "2.15.*",
"friendsofsymfony/rest-bundle": "2.5.*",
"jms/serializer-bundle": "1.1.*"
Route Overrides:
routing.yml), ensure the prefix and resource paths match the bundle’s structure (@AisMahasiswaProfileBundle/Resources/config/routes.yml).API Doc Issues:
/api/doc fails, verify:
NelmioApiDocBundle is registered in AppKernel.php.routing.yml includes the NelmioApiDocBundle route.app/logs/dev.log).Serialization Errors:
JMSSerializerBundle configuration (config.yml) for correct metadata paths.php app/console cache:clear
Doctrine Migrations:
MahasiswaProfile entity schema changes, create a migration:
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
Extending Entities:
php app/console doctrine:schema:update --force
namespace AppBundle\Entity;
use Ais\MahasiswaProfileBundle\Entity\MahasiswaProfile as BaseProfile;
class MahasiswaProfile extends BaseProfile {
/**
* @ORM\Column(type="string")
*/
private $extensionField;
}
Testing:
LiipFunctionalTestBundle (dev dependency) to write functional tests for API endpoints.namespace Tests\Ais\MahasiswaProfileBundle;
use Liip\FunctionalTestBundle\Test\WebTestCase;
class MahasiswaProfileTest extends WebTestCase {
public function testGetProfile() {
$client = static::createClient();
$client->request('GET', '/api/mahasiswaprofiles/1');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Performance:
findBy with pagination):
$repository->findBy(array(), null, 10, 0); // Limit 10 results
Custom Validation:
@Assert\Length):
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @Assert\Callback(methods={"validateCustomRules"})
*/
class MahasiswaProfile {
// ...
public function validateCustomRules($context) {
// Custom validation
How can I help you explore Laravel packages today?