1. Download Symfony2 Standard Edition
2. extract :
tar
-zxvf Symfony_Standard_Vendors_2.0.0BETA2.tgz
3. set permission :
chmod
777 app
/cache
app
/logs
4.
Point your server to “web/” as root directory and you already would have to see symfony2 welcome page at http://127.0.0.1/app_dev.php/
5. setup database config:
vi
app/config/parameter.yml
6.
Creating our first bundleASF
/Bundle/StoreBundle --no-interaction
7. add
to app/AppKernel.php :
$bundles
=
array
(
// ...
new
ASF
\StoreBundle\ASFStoreBundle(),
);
8. add to app/config/routing.yml
asf_store:
resource: "@ASFStoreBundle/Resources/config/routing.yml"
prefix: /store
9. Creating a controller :
vi src/ASF/StoreBundle/Controller/StoreController.php
//ASF/StoreBundle/Controller/StoreController.php
<?php
namespace
ASF
\StoreBundle\Controller;
use
Symfony\Bundle\FrameworkBundle\Controller\Controller;
use
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class
StoreController
extends
Controller
{
/**
* @Template()
*/
public
function
indexAction(
$store
)
{
return
array
(
'store'
=>
$store
);
}
}
10. add to src/ASF/StoreBundle/Resources/config/routing.yml
asf_mystore:
pattern: /store/{store}
defaults: { _controller: ASFStoreBundle:Store:index }
11. add a view
vi src/ASF/StoreBundle/Resources/views/Store/index.html.twig
So you want store "{{ store }}"?
12. Done
going to http://127.0.0.1/app_dev.php/store/my-store
13. Model
- define a model store/category
- Store model
- vi
ASF/StoreBundle/Entity/Store.php
<?php
// ASF/storeBundle/Entity/Store.php
namespace
ASF
\StoreBundle\Entity;
use
Doctrine\ORM\Mapping
as
ORM;
/**
* @ORM\Entity
*/
class
Store
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected
$id
;
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="stores_categories",
* joinColumns={@ORM\JoinColumn(name="store_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
*/
protected
$categories
;
/**
* @ORM\Column(type="string", length=255)
*/
protected
$url
;
/**
* @ORM\Column(type="string", length=255)
*/
protected
$name
;
/**
* @ORM\Column(type="integer")
*/
protected
$clicks
= 0;
/**
* @ORM\Column(type="boolean")
*/
protected
$validated
= false;
/**
* @ORM\Column(type="integer")
*/
protected
$pcomments
= 0;
/**
* @ORM\Column(type="integer")
*/
protected
$ncomments
= 0;
/**
* @ORM\Column(type="boolean")
*/
protected
$active
= false;
/**
* @ORM\Column(type="datetime", name="updated_at")
*/
protected
$updatedAt
;
/**
* @ORM\Column(type="datetime", name="created_at")
*/
protected
$createdAt
;
public
function
__construct()
{
$this
->categories =
new
\Doctrine\Common\Collections\ArrayCollection();
$this
->createdAt =
new
\DateTime();
$this
->updatedAt =
new
\DateTime();
}
}
- Store model
- vi
ASF/StoreBundle/Entity/Category.php
<?php
// ASF/StoreBundle/Entity/Category.php
namespace
ASF
\StoreBundle\Entity;
use
Doctrine\ORM\Mapping
as
ORM;
/**
* @ORM\Entity
*/
class
Category
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected
$id
;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
protected
$children
;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
* @ORM\Column(nullable=true)
*/
protected
$parent
;
/**
* @ORM\ManyToMany(targetEntity="Store", mappedBy="categories")
*/
protected
$stores
;
/**
* @ORM\Column(type="string", length=255)
*/
protected
$name
;
/**
* @ORM\Column(type="string", length=255, name="url_string", unique=true)
*/
protected
$urlString
;
public
function
__construct()
{
$this
->stores =
new
\Doctrine\Commmon\Collections\ArrayCollection();
}
}
Creating Tables :
php app
/console
doctrine:schema:create
Creating Getters & Setters
php app
/console
doctrine:generate:entities ASFStoreBundle
14. Test the module
add to the routing the storeAction:
vi src/ASF/StoreBundle/Resources/config/routing.yml
asf_mystore:
pattern: /store/{store}
defaults: { _controller: ASFStoreBundle:Store:store }
asf_mystore_index:
pattern: /store/
defaults: { _controller: ASFStoreBundle:Store:index }
vi src/ASF/StoreBundle/Controller/StoreController.php
<?php
//ASF/StoreBundle/Controller/StoreController.php
namespace ASF\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class StoreController extends Controller
{
/**
* @Template()
*/
public function indexAction()
{
$em = $this->get('doctrine.orm.entity_manager');
$stores = $em->createQuery('SELECT count(s.id) AS total FROM ASF\StoreBundle\Entity\Store s')->getSingleScalarResult();
return array('stores' => $stores);
}
/**
* @Template()
*/
public function storeAction($store)
{
return array('store' => $store);
}
}
vi src/ASF/StoreBundle/Resources/views/Store/index.html.twig
vi src/ASF/StoreBundle/Resources/views/Store/store.html.twig
add to the routing the storeAction:
vi src/ASF/StoreBundle/Resources/config/routing.yml
asf_mystore:
pattern: /store/{store}
defaults: { _controller: ASFStoreBundle:Store:store }
asf_mystore_index:
pattern: /store/
defaults: { _controller: ASFStoreBundle:Store:index }
vi src/ASF/StoreBundle/Controller/StoreController.php
<?php
//ASF/StoreBundle/Controller/StoreController.php
namespace ASF\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class StoreController extends Controller
{
/**
* @Template()
*/
public function indexAction()
{
$em = $this->get('doctrine.orm.entity_manager');
$stores = $em->createQuery('SELECT count(s.id) AS total FROM ASF\StoreBundle\Entity\Store s')->getSingleScalarResult();
return array('stores' => $stores);
}
/**
* @Template()
*/
public function storeAction($store)
{
return array('store' => $store);
}
}
vi src/ASF/StoreBundle/Resources/views/Store/index.html.twig
<!-- Montes/AdictosBundle/Resources/views/Store/index.html.twig -->
We have a total of {{ stores }} stores.
vi src/ASF/StoreBundle/Resources/views/Store/store.html.twig
<!-- Montes/AdictosBundle/Resources/views/Store/store.html.twig -->
So you want store "{{ store }}"?
No comments:
Post a Comment