Coding

I code
You code…

Inject a custom Controller in Sonata

I had this problem, I needed to inject a Storage to my controller so I can create a custom action and connect to my db using a storage service.

All I needed was configure the services as following:

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
services:
  app.admin.document
:
    class
: AppBundle\Admin\DocumentAdmin
    arguments
: [~, AppBundle\Entity\Document, 'app.controller.document']
    tags
:
      - { name
: sonata.admin, manager_type: orm, group: Documentation, label: Documents }

app.controller.document
:
    class
: AppBundle\Controller\DocumentAdminController
    calls
:
      - [ setContainer,[ "@service_container" ] ]
    arguments
:
      - "@security.token_storage"
      - "@app.storage.document"

app.repository.document
:
    class
: AppBundle\Repository\DocumentRepository
    factory
: ["@doctrine.orm.entity_manager", getRepository]
    arguments
:
      - AppBundle\Entity\Document

app.storage.document
:
    class
: AppBundle\Storage\DocumentStorage
    arguments
:
      - "@app.repository.document"

Ignore modified file from commit

Sometimes you need to change some files, just for your development environment and you don’t want to commit those changes. With Git there a way to fix your problem.

First of all, made all the change you need in the file(s) to ignore then run this command:

1
$ git update-index --assume-unchanged path/to/the/file/to/ignore

and it’s done, now your changes are ignored.

Sure, you can roolback those changes by running the command

1
$ git update-index --no-assume-unchanged path/to/the/file/to/version/again

At any point, if you feel that you lost the count of the untracked files, you can list all the files versioned with the files the that starts with a lowercase letter are ignored from versioning.

1
git ls-files -v

To simplify the output run this command, it will only show the ignored files:

1
git ls-files -v | grep '^[[:lower:]]'

Install FOS User in Symfony 2

After finished the tutorial I needed a user Bundle, the FOS Bundle (Friend of Symfony) is for Symfony 2 what SfGuardUser was for Symfony 1, so we gonna continue the tutorial started in the previous post.

Installing the bunddles:

1
2
composer require sonata-project/user-bundle --no-update
composer update

Add to your registerbundles function in the AppKernel class

1
2
3
new FOSUserBundleFOSUserBundle(),
new SonataUserBundleSonataUserBundle('FOSUserBundle'),
new ApplicationSonataUserBundleApplicationSonataUserBundle(),

Add to the config.yml

1
2
3
4
5
6
7
8
9
10
11
12
sonata_user:
    security_acl
: true
    manager_type
: orm # can be orm or mongodb
fos_user
:
    db_driver
: orm # can be orm or odm
    firewall_name
: main
    user_class
: ApplicationSonataUserBundleEntityUser
    group
:
        group_class
: ApplicationSonataUserBundleEntityGroup
        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)

In the config.yml, add to the doctrine configuration under the dbal option the types lines

1
2
types:
    json
: SonataDoctrineTypesJsonType

Change the security.yml for this one

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
security:

    encoders
:
        FOSUserBundleModelUserInterface
: sha512

    acl
:
        connection
: default

    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_manager

    firewalls
:
       # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev
:
            pattern
: ^/(_(profiler|wdt)|css|images|js)/
            security
: false

        # -> custom firewall for the admin area of the URL
        admin
:
            pattern
:           /admin(.*)
            context
:           user
            form_login
:
                provider
:      fos_userbundle
                login_path
:    /admin/login
                use_forward
:   false
                check_path
:    /admin/login_check
                failure_path
:  null
            logout
:
                path
:          /admin/logout
            anonymous
:         true

        # -> end custom configuration

        # default login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main
:
            pattern
:            .*
            context
:            user
            form_login
:
                provider
:      fos_userbundle
                login_path
:    /login
                use_forward
:   false
                check_path
:    /login_check
                failure_path
:  null
            logout
:            true
            anonymous
:         true

        default
:
            anonymous
: ~


    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 }

Update the 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
app:
    resource
: "@AppBundle/Controller/"
    type
:    annotation

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

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

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

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

Now generate the entities

1
php app/console sonata:easy-extends:generate SonataUserBundle -d src

You can clone the project from my github account here.

Shell “ls -l” sort by list column

It’s common to list the files on directory, but it usefull to list them order sometimes. One way to do that is to simply add the | sort -k instruction. The value of k is the column who will be ordered.

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
15:42 $ ls -lh
total 712
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir1
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir2
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir3
-rw-r--r--  1 walter  staff   1.6K Jan 19 15:40 file1.txt
-rw-r--r--  1 walter  staff    68K Jan 19 15:40 file2.txt
-rw-r--r--  1 walter  staff   139K Jan 19 15:41 file3.txt
-rw-r--r--  1 walter  staff    70K Jan 19 15:41 file4.txt
-rw-r--r--  1 walter  staff    71K Jan 19 15:42 file5.txt

15:42 $ ls -lh | sort -k5
total 712
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir1
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir2
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir3
-rw-r--r--  1 walter  staff    68K Jan 19 15:40 file2.txt
-rw-r--r--  1 walter  staff    70K Jan 19 15:41 file4.txt
-rw-r--r--  1 walter  staff    71K Jan 19 15:42 file5.txt
-rw-r--r--  1 walter  staff   1.6K Jan 19 15:40 file1.txt
-rw-r--r--  1 walter  staff   139K Jan 19 15:41 file3.txt

15:43 $ ls -lh | sort -k5n
total 712
-rw-r--r--  1 walter  staff   1.6K Jan 19 15:40 file1.txt
-rw-r--r--  1 walter  staff    68K Jan 19 15:40 file2.txt
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir1
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir2
drwxr-xr-x  2 walter  staff    68B Jan 19 15:39 dir3
-rw-r--r--  1 walter  staff    70K Jan 19 15:41 file4.txt
-rw-r--r--  1 walter  staff    71K Jan 19 15:42 file5.txt
-rw-r--r--  1 walter  staff   139K Jan 19 15:41 file3.txt

Parameters explanation:

1
2
3
4
  -k, --key=POS1[,POS2]
         start a key at POS1, end it at POS2 (origin 1)
  -n, --numeric-sort
         compare according to string numerical value

View the full sort manual here