Skip to content

Instantly share code, notes, and snippets.

@elnur
Last active August 29, 2015 14:01
Show Gist options
  • Save elnur/36cd4f5d28606acb2477 to your computer and use it in GitHub Desktop.
Save elnur/36cd4f5d28606acb2477 to your computer and use it in GitHub Desktop.
Symfony Without Bundles
jms_di_extra:
locations:
directories: "%kernel.root_dir%/../src"
doctrine:
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
mappings:
model:
type: annotation
dir: "%kernel.root_dir%/../src/Example/Model"
prefix: Example\Model
alias: Model
is_bundle: false
site:
resource: Example\Controller\UserController
type: annotation
services:
user_manager:
class: Example\Manager\UserManager
arguments:
- "@user_repository"
<?php
namespace Example\Model;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
/**
* @Entity
*/
class User
{
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*
* @var int
*/
private $id;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
}
<?php
namespace Example\Controller;
use Example\Manager\UserManager;
use Example\Model\User;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Service("user_controller")
* @Route("/user", service="user_controller")
*/
class UserController
{
/**
* @var UserManager
*/
private $userManager;
/**
* @InjectParams
*
* @param UserManager $userManager
*/
public function __construct(UserManager $userManager)
{
$this->userManager = $userManager;
}
/**
* @Route("/{id}")
*
* @param int $id
* @return User
*/
public function viewAction($id)
{
return $this->userManager->find($id);
}
}
<?php
namespace Example\Controller;
use Example\Manager\UserManager;
use Example\Model\User;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Service("user_controller")
* @Route("/user", service="user_controller")
*/
class UserController
{
/**
* @var UserManager
*/
private $userManager;
/**
* @InjectParams
*
* @param UserManager $userManager
*/
public function __construct(UserManager $userManager)
{
$this->userManager = $userManager;
}
/**
* @Route("/{id}")
* @Template
*
* @param int $id
* @return User
*/
public function viewAction($id)
{
return [
'user' => $this->userManager->find($id),
];
}
}
<?php
namespace Example\Manager;
use Example\Model\User;
use Example\Repository\UserRepository;
class UserManager
{
/**
* @var UserRepository
*/
private $userRepository;
/**
* @param UserRepository $userRepository
*/
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param int $id
* @return User
*/
public function find($id)
{
return $this->userRepository->find($id);
}
}
<?php
namespace Example\Manager;
use Example\Model\User;
use Example\Repository\UserRepository;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
/**
* @Service("user_manager")
*/
class UserManager
{
/**
* @var UserRepository
*/
private $userRepository;
/**
* @InjectParams
*
* @param UserRepository $userRepository
*/
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param int $id
* @return User
*/
public function find($id)
{
return $this->userRepository->find($id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment