Raffaello Bertini Blog

Articles about IT stuff and more!

Symfony2 Integrate FOSUserBundle, SonataAdminBundle, SonataUserBundle

| Comments

Install symfony2 and the dependencies

This symfony2 project require some 3rd parties bundles that can be easily installed using composer require [package ... ]. It use doctrine ORM for mapping entities, check on-line documentations for other possibilities: http://sonata-project.org/bundles/admin/2-3/doc/reference/installation.html

The final composer.json will look like:

composer.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
"require": {
  "php": ">=5.3.3",
  "symfony/symfony": "2.3.*",
  "doctrine/orm": "~2.2,>=2.2.3",
  "doctrine/doctrine-bundle": "1.2.*",
  "twig/extensions": "1.0.*",
  "symfony/assetic-bundle": "2.3.*",
  "symfony/swiftmailer-bundle": "2.3.*",
  "symfony/monolog-bundle": "2.3.*",
  "sensio/distribution-bundle": "2.3.*",
  "sensio/framework-extra-bundle": "2.3.*",
  "sensio/generator-bundle": "2.3.*",
  "incenteev/composer-parameter-handler": "~2.0",
  "friendsofsymfony/user-bundle": "~1.3",
  "sonata-project/admin-bundle": "~2.3",
  "sonata-project/intl-bundle": "~2.2",
  "jms/translation-bundle": "~1.1",
  "sonata-project/doctrine-orm-admin-bundle": "~2.3",
  "sonata-project/user-bundle": "~2.2",
  "knplabs/knp-menu-bundle": "1.1.*",
  "mopa/bootstrap-bundle": "~2.3"
  },
...

It can be accomplished running the following command-line:

1
2
3
4
5
6
composer.phar require 'friendsofsymfony/user-bundle'
composer.phar require 'sonata-project/admin-bundle'
composer.phar require 'sonata-project/doctrine-orm-admin-bundle'
composer.phar require 'sonata-project/user-bundle'
composer.phar require 'knplabs/knp-menu-bundle 1.1.*'
composer.phar require 'mopa/bootstrap-bundle'
  • The mopa/bootstrap-bundle is required for the standard template used in SonataUserBundle.
  • The knplabs/knp-menu-bundle 1.1* is required because the master branch is in refactoring phase.

Creating the Admin Blog Bundle

Create a bundle for the blog’s post in the Admin dashboard.

1
php app/console generate:Bundle --namespacce=Acme/BlogAdminBundle --dir=src --bundle-name=BlogAdminBundle

Enabling the bundle

After composer has finished its task, it is required to enable the bundles in ‘app/AppKernel.php’ :

% AppKernel.php
1
2
3
4
5
6
7
8
9
10
new \Mopa\Bundle\BootstrapBundle\MopaBootstrapBundle(),
new \FOS\UserBundle\FOSUserBundle(),
new Sonata\CoreBundle\SonataCoreBundle(),
new Sonata\BlockBundle\SonataBlockBundle(),
new Knp\Bundle\MenuBundle\KnpMenuBundle(),
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
new Sonata\AdminBundle\SonataAdminBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Acme\BlogAdminBundle\AcmeBlogAdminBundle(),
new Sonata\EasyExtendsBundle\SonataEasyExtendsBundle()

Extending Sonata User Bundle

Now we extend the SonataUserBundle to easily customize it using Sonata:easy-extends command

%
1
php app/console sonata:easy-extends:generate SonataUserBundle --dest=src

The bundle will be in the Application/Sonata namespace.

configure FOS/UserBundle

the file app/config/config.yml it looks like:

config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fos_user:
  db_driver: orm
  firewall_name: main
  user_class:     Application\Sonata\UserBundle\Entity\User
  service:
    mailer: fos_user.mailer.twig_swift
  registration:
    confirmation:
      from_email:
        address: registration@UserBundle.org
        sender_name: User Bundle registration
      #enabled: true
  resetting:
    email:
      from_email:
        address: resetting@UserBundle.org
        sender_name: User Bundle resetting
      template: ApplicationSonataUserBundle:User:resetting.email.twig

  from_email:
    address: noreply@UserBundle.org
    sender_name: User Bundle

  group:
    group_class:   Application\Sonata\UserBundle\Entity\Group
    group_manager: sonata.user.orm.group_manager
    # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)

  service:
    user_manager: sonata.user.orm.user_manager
    # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)

configure sonata/admin

After that, configure the ‘sonata_admin’ bundle like this:

app/config/config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sonata_admin:
  title: Administration
  #title_logo: bundles/images/logo.png
  dashboard:
    blocks:
      -
        position: left
        type: sonata.admin.block.admin_list
      -
        position: right
        type: sonata.block.service.text
        settings:
          content: >
            <h2>Welcome to the Sonata Admin</h2>
            <p>This is a <code>sonata.block.service.text</code> from the Block
            Bundle, you can create and add new block in these area by configuring
            the <code>sonata_admin</code> section.</p> <br /> For instance, here
            a RSS feed parser (<code>sonata.block.service.rss</code>):
      -
        position: right
        type: sonata.block.service.rss
        settings:
          title: Sonata Project's Feeds
          url: http://sonata-project.org/blog/archive.rss

creating Admin class

In the AdminBlogBundle create a file called:

  • PostAdmin.php

After that, paste the following code:

PostAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace Acme\AdminBlogBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class PostAdmin extends Admin
{
  // Fields to be shown on create/edit forms
  protected function configureFormFields(FormMapper $formMapper)
  {
    $formMapper
    ->add('title')
    ->add('author')
    ->add('content') //if no type is specified, SonataAdminBundle tries to guess it
    ;
  }

  // Fields to be shown on filter forms
  protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  {
    $datagridMapper
    ->add('title')
    ->add('author')
    ;
  }

  // Fields to be shown on lists
  protected function configureListFields(ListMapper $listMapper)
  {
    $listMapper
    ->addIdentifier('title')
    ->add('slug')
    ->add('author')
    ;
  }
}

Configure Admin services

create the file admin.yml inside AdminBlogBundle\Resources\config\admin.yml

admin.yml
1
2
3
4
5
6
7
8
9
10
11
12
services:
    sonata.admin.post:
        class: Acme\BlogBundle\Admin\PostAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Post" }
        arguments:
            - ~
            - Acme\BlogBundle\Entity\Post
            - SonataAdminBundle:CRUD
            - ~
        calls:
            - [ setLabelTranslatorStrategy, ["@sonata.admin.label.strategy.underscore"]]

configure sonataUser

follow the wiki here for more details: http://sonata-project.org/bundles/user/2-2/doc/index.html

paste the following chunk in the app/config/config.yml

app/config/config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: @AcmeAdminBlogBundle/Resources/config/admin.yml }

sonata_user:
    profile:
        # Profile show page is a dashboard as in SonataAdminBundle
        dashboard:
            blocks:
                -
                    position: left
                    type: sonata.admin.block.admin_list
                -
                    position: right
                    type: sonata.block.service.text
                    settings:
                        content: >
                            <h2>Welcome!</h2>
                            This is a sample user profile dashboard,
                            feel free to override it in the configuration!
                            Want to make this text dynamic?
                            For instance display the user's name?
                            Create a dedicated block and edit the configuration!

          # Customize user portal menu by setting links
        menu:
            - { route: 'sonata_user_profile_show', label: 'sonata_profile_title', domain: 'SonataUserBundle'}
            - { route: 'sonata_user_profile_edit', label: 'link_edit_profile', domain: 'SonataUserBundle'}
            - { route: 'sonata_user_profile_edit_authentication', label: 'link_edit_authentication', domain: 'SonataUserBundle'}
            - { route: 'sonata_admin_redirect', label: 'Admin section', domain: 'SonataAdminBundle' }

configure the whole application

We have to configure the database parameters, paste in the app/config/parameters.yml the following code:

parameters.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## This file is auto-generated during the composer install
parameters:
  database_driver: pdo_sqlite
  database_host: 127.0.0.1
  database_port: null
  database_name: symfony
  database_user: root
  database_password: null
  mailer_transport: smtp
  mailer_host: 127.0.0.1
  mailer_user: null
  mailer_password: null
  locale: en
  secret: ThisTokenIsNotSoSecretChangeIt
  database_path: '%kernel.root_dir%/../sql/data.db3'

routing

We need to insert the routing of ours bundles in app/config/routing.yml:

routing.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
acme_admin_blog:
    resource: "@AcmeAdminBlogBundle/Resources/config/routing.yml"
    prefix:   /admin/acme/adminBlog

application_sonata_user:
    resource: "@ApplicationSonataUserBundle/Resources/config/routing.yml"
    prefix:   /

sonata_user_security:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_security_1.xml"

sonata_user_resetting:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_resetting_1.xml"
    prefix: /resetting

sonata_user_profile:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_profile_1.xml"
    prefix: /profile

sonata_user_register:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_registration_1.xml"
    prefix: /register

sonata_user_change_password:
    resource: "@SonataUserBundle/Resources/config/routing/sonata_change_password_1.xml"
    prefix: /profile

admin:
    resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix: /admin

sonata_user:
    resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
    prefix: /admin

_sonata_admin:
    resource: .
    type: sonata_admin
    prefix: /admin

insert the following route in the AdminBlogBundle:

routing.yml
1
2
3
AcmeBlogAdminBundle_post:
  resource: "@AcmeBlogAdminBundle/Resources/config/routing/post.yml"
  prefix:   /post

other symfony configuration

In the app/config/config.yml add:

config.yml
1
2
3
4
doctrine:
  path:     %database_path%
  types:
    json: Sonata\Doctrine\Types\JsonType

In the app/config/security.yml paste the following code:

security.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
security:
  encoders:
    #Symfony\Component\Security\Core\User\User: plaintext
    FOS\UserBundle\Model\UserInterface: sha512

  role_hierarchy:
    ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
    ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    SONATA:
    #- ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  
    # if you are using acl then this line must be commented

  providers:
    fos_userbundle:
      id: fos_user.user_provider.username

  firewalls:
    main:
      pattern: ^/
      context: user
      form_login:
        provider: fos_userbundle
        csrf_provider: form.csrf_provider
        login_path: /login
        use_forward: false
        check_path: /login_check
        failure_path: null
      logout: true
      anonymous: true

    dev:
      pattern:  ^/(_(profiler|wdt)|css|images|js)/
      security: false

    admin:
      pattern: /admin(.*)
      context: user
      form_login:
        provider: fos_userbundle
        login_path: /admin/login
        use_forward: false
        check_path: /admin/login_checl
        failure_path: null
      logout:
        path: /admin/logout
      anonymous: true

  access_control:
    # URL of FOSUserBundle which need to be available to anonymous users
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

    # Admin login page needs to be access without credential
    - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

    # Secured part of the site
    # This config requires being logged for the whole site and having the admin role for the admin part.
    # Change these rules to adapt them to your needs
    - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
    - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

generate entities for Blog

Create the entities inside BlogAdmin\Entity\:

  • Post.php
  • Comment.php
  • Tag.php
Post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php

namespace Acme\AdminBlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Post
{
  /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  protected $id;

  /**
  * @ORM\Column(type="string", length=255)
  *
  * @Assert\NotBlank()
  * @Assert\Length(min="10", max=255)
  */
  protected $title;

  /**
  * @ORM\Column(type="text")
  */
  protected $abstract;

  /**
  * @ORM\Column(type="text")
  *
  * @Assert\NotBlank()
  */
  protected $content;

  /**
  * @ORM\Column(type="boolean")
  */
  protected $enabled;

  /**
  * @ORM\Column(type="datetime")
  */
  protected $created_at;

  /**
  * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
  */
  protected $comments;

  /**
  * @ORM\ManyToMany(targetEntity="Tag")
  */
  protected $tags;

  /**
  *
  * @ORM\ManyToOne(targetEntity="\Acme\UserBundle\Entity\User")
  * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
  */
  protected $author;

  public function __construct()
  {
    $this->tags     = new \Doctrine\Common\Collections\ArrayCollection();
    $this->comments = new \Doctrine\Common\Collections\ArrayCollection();
    $this->created_at = new \DateTime("now");
  }

  public function __toString()
  {
    return $this->getTitle();
  }

  /**
  * Get id
  *
  * @return integer
  */
  public function getId()
  {
    return $this->id;
  }

  /**
  * Set title
  *
  * @param string $title
  * @return Post
  */
  public function setTitle($title)
  {
    $this->title = $title;

    return $this;
  }

  /**
  * Get title
  *
  * @return string
  */
  public function getTitle()
  {
    return $this->title;
  }

  /**
  * Set abstract
  *
  * @param string $abstract
  * @return Post
  */
  public function setAbstract($abstract)
  {
    $this->abstract = $abstract;

    return $this;
  }

  /**
  * Get abstract
  *
  * @return string
  */
  public function getAbstract()
  {
    return $this->abstract;
  }

  /**
  * Set content
  *
  * @param string $content
  * @return Post
  */
  public function setContent($content)
  {
    $this->content = $content;

    return $this;
  }

  /**
  * Get content
  *
  * @return string
  */
  public function getContent()
  {
    return $this->content;
  }

  /**
  * Set enabled
  *
  * @param boolean $enabled
  * @return Post
  */
  public function setEnabled($enabled)
  {
    $this->enabled = $enabled;

    return $this;
  }

  /**
  * Get enabled
  *
  * @return boolean
  */
  public function getEnabled()
  {
    return $this->enabled;
  }

  /**
  * Set created_at
  *
  * @param \DateTime $createdAt
  * @return Post
  */
  public function setCreatedAt($createdAt)
  {
    $this->created_at = $createdAt;

    return $this;
  }

  /**
  * Get created_at
  *
  * @return \DateTime
  */
  public function getCreatedAt()
  {
    return $this->created_at;
  }

  /**
  * Add comments
  *
  * @param \Acme\BlogBundle\Entity\Comment $comments
  * @return Post
  */
  public function addComment(\Acme\BlogBundle\Entity\Comment $comments)
  {
    $this->comments[] = $comments;

    return $this;
  }

  /**
  * Remove comments
  *
  * @param \Acme\BlogBundle\Entity\Comment $comments
  */
  public function removeComment(\Acme\BlogBundle\Entity\Comment $comments)
  {
    $this->comments->removeElement($comments);
  }

  /**
  * Get comments
  *
  * @return \Doctrine\Common\Collections\Collection
  */
  public function getComments()
  {
    return $this->comments;
  }

  /**
  * Add tags
  *
  * @param \Acme\BlogBundle\Entity\Tag $tags
  * @return Post
  */
  public function addTag(\Acme\BlogBundle\Entity\Tag $tags)
  {
    $this->tags[] = $tags;

    return $this;
  }

  /**
  * Remove tags
  *
  * @param \Acme\BlogBundle\Entity\Tag $tags
  */
  public function removeTag(\Acme\BlogBundle\Entity\Tag $tags)
  {
    $this->tags->removeElement($tags);
  }

  /**
  * Get tags
  *
  * @return \Doctrine\Common\Collections\Collection
  */
  public function getTags()
  {
    return $this->tags;
  }

  /**
  * Set author
  *
  * @param \Acme\UserBundle\Entity\User $author
  * @return Post
  */
  public function setAuthor(\Acme\UserBundle\Entity\User $author = null)
  {
    $this->author = $author;

    return $this;
  }

  /**
  * Get author
  *
  * @return \Acme\UserBundle\Entity\User
  */
  public function getAuthor()
  {
    return $this->author;
  }
}
Comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

namespace Acme\AdminBlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Comment
{
  /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  protected $id;

  /**
  * @ORM\Column(type="string")
  *
  * @Assert\NotBlank()
  */
  protected $name;


  /**
  * @ORM\Column(type="string")
  *
  * @Assert\NotBlank()
  */
  protected $email;

  /**
  * @ORM\Column(type="string")
  */
  protected $url;

  /**
  * @ORM\Column(type="text")
  * @Assert\NotBlank()
  */
  protected $message;

  /**
  * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
  */
  protected $post;

  public function __toString()
  {
    return $this->getName();
  }

  /**
  * Get id
  *
  * @return integer
  */
  public function getId()
  {
    return $this->id;
  }

  /**
  * Set name
  *
  * @param string $name
  * @return Comment
  */
  public function setName($name)
  {
    $this->name = $name;

    return $this;
  }

  /**
  * Get name
  *
  * @return string
  */
  public function getName()
  {
    return $this->name;
  }

  /**
  * Set email
  *
  * @param string $email
  * @return Comment
  */
  public function setEmail($email)
  {
    $this->email = $email;

    return $this;
  }

  /**
  * Get email
  *
  * @return string
  */
  public function getEmail()
  {
    return $this->email;
  }

  /**
  * Set url
  *
  * @param string $url
  * @return Comment
  */
  public function setUrl($url)
  {
    $this->url = $url;

    return $this;
  }

  /**
  * Get url
  *
  * @return string
  */
  public function getUrl()
  {
    return $this->url;
  }

  /**
  * Set message
  *
  * @param string $message
  * @return Comment
  */
  public function setMessage($message)
  {
    $this->message = $message;

    return $this;
  }

  /**
  * Get message
  *
  * @return string
  */
  public function getMessage()
  {
    return $this->message;
  }

  /**
  * Set post
  *
  * @param \Acme\BlogBundle\Entity\Post $post
  * @return Comment
  */
  public function setPost(\Acme\BlogBundle\Entity\Post $post = null)
  {
    $this->post = $post;

    return $this;
  }

  /**
  * Get post
  *
  * @return \Acme\BlogBundle\Entity\Post
  */
  public function getPost()
  {
    return $this->post;
  }
}
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace Acme\AdminBlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity
*/
class Tag
{
  /**
  * @ORM\Id
  * @ORM\Column(type="integer")
  * @ORM\GeneratedValue(strategy="AUTO")
  */
  protected $id;

  /**
  * @ORM\Column(type="string")
  * @Assert\NotBlank()
  */
  protected $name;

  /**
  * @ORM\Column(type="boolean")
  */
  protected $enabled;

  /**
  * @ORM\ManyToMany(targetEntity="Post")
  */
  protected $posts;

  public function __construct()
  {
    $this->posts = new \Doctrine\Common\Collections\ArrayCollection();
  }

  public function __toString()
  {
    return $this->getName();
  }

  /**
  * Get id
  *
  * @return integer
  */
  public function getId()
  {
    return $this->id;
  }

  /**
  * Set name
  *
  * @param string $name
  * @return Tag
  */
  public function setName($name)
  {
    $this->name = $name;

    return $this;
  }

  /**
  * Get name
  *
  * @return string
  */
  public function getName()
  {
    return $this->name;
  }

  /**
  * Set enabled
  *
  * @param boolean $enabled
  * @return Tag
  */
  public function setEnabled($enabled)
  {
    $this->enabled = $enabled;

    return $this;
  }

  /**
  * Get enabled
  *
  * @return boolean
  */
  public function getEnabled()
  {
    return $this->enabled;
  }

  /**
  * Add posts
  *
  * @param \Acme\BlogBundle\Entity\Post $posts
  * @return Tag
  */
  public function addPost(\Acme\BlogBundle\Entity\Post $posts)
  {
    $this->posts[] = $posts;

    return $this;
  }

  /**
  * Remove posts
  *
  * @param \Acme\BlogBundle\Entity\Post $posts
  */
  public function removePost(\Acme\BlogBundle\Entity\Post $posts)
  {
    $this->posts->removeElement($posts);
  }

  /**
  * Get posts
  *
  * @return \Doctrine\Common\Collections\Collection
  */
  public function getPosts()
  {
    return $this->posts;
  }
}

console commands

In order to make the application working we need to run some scripts into the console and set up the permissions for Apache.

Setting up the DataBase

run the following console commands:

1
2
3
4
php app/console doctrine:database:create
php app/console doctrine:schema:create
php app/console fos:user:create //admin
php app/console fos:user:promote //ROLE_ADMIN

Setting up the web application

run:

1
php app/console assets:install

Setting Permissions

we have to give the permissions to Apache for the following file/folder:

  • app/cache
  • app/logs
  • sql/
  • sql/data.db3

run the following commands:

1
2
sudo chown -R apache.apache app/cache app/logs sql
sudo chmod -R 775 app/cache app/logs sql

remember after that you have to perform other operation with root or apache user or insert into apache user.

P.S.

Maybe you have to replace apache with www-data depending on the distro you’re using.

browse application

Now if you browse your application in route app_dev.php/login you can enter as admin in your application and see the dashboard in app_dev.php/admin.

Conclusion

I illustraded in a wide way how can set up a first web application.

You can see the repository here: https://github.com/Raffaello/symfony2-example.git

The second part is here: http://raffaello.github.io/blog/2015/01/28/symfony2-customize-sonatauserbundle/

Comments