Web development and stuff

Latest Posts

Install and configure Symfony 2 and Sonata bundle

I found some bugs and inconsistencies while trying to install those 3 bundles by the book, so I decide to create that small tutorial.

What we gonna install:

  • Symfony Framework
  • SonataAdminBundle
  • SonataDoctrineORMAdminBundle
  • SonataEasyExtendsBundle

First we start by installing the Symfony Framework

1
composer create-project symfony/framework-standard-edition

Now we jump to the sonata bundle

1
composer require sonata-project/admin-bundle

Choose one connection, I use mysql so I need to install the SonataDoctrineORMAdminBundle

1
composer require sonata-project/doctrine-orm-admin-bundle

Activate the bundles

SonataAdminBundle

AppKernel.php

1
2
3
4
5
6
7
/* Sonata */
new SymfonyBundleSecurityBundleSecurityBundle(),
new SonataCoreBundleSonataCoreBundle(),
new SonataBlockBundleSonataBlockBundle(),
new KnpBundleMenuBundleKnpMenuBundle(),
new SonataDoctrineORMAdminBundleSonataDoctrineORMAdminBundle(),
new SonataAdminBundleSonataAdminBundle(),

config.yml

1
2
3
4
5
6
sonata_block:
  default_contexts
: [cms]
  blocks
:
   # Enable the SonataAdminBundle block
    sonata.admin.block.admin_list
:
      contexts
:  [admin]

routing.yml

1
2
3
4
5
6
7
8
admin:
    resource
: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
    prefix
: /admin

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

With this done, you should see a layout like this one in your site.

Sonata admin dashboard

At this moment we have the Sonata Admin Bundle configured in our site. We gonna process now with the Easy Extend Bundle then we end with the User Bundle.

EasyExtendBundle

Pretty simple to configure this bundle, start to download the bundle and activate the bundle in the AppKernel file.

1
php require sonata-project/easy-extends-bundle

AppKernel.yml

1
new SonataEasyExtendsBundleSonataEasyExtendsBundle(),

MySQL create databases and user from a to z

How can I create a database and an user to work together in MySQL. Well, it’s quit easy, theres the few MySQL lines you need to use to solve your problem.

Create the database
Create an user
1
CREATE USER 'my_user'@'localhost' IDENTIFIED BY 'my_pass';
Verify user
1
SELECT * FROM mysql.user WHERE User = 'my_user'G
Grant privileges to user
1
GRANT ALL ON my_db.* TO 'my_user'@'localhost';
Verify user GRANTS
1
SHOW GRANTS FOR my_user@localhost;

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

htaccess redirect all subdomain except one

I needed a rule to redirect all subdomain in my domain except one specified subdomain, to do that I use those few lines in my .htaccess file

1
2
3
4
5
6
7
8
<IfModule mod_rewrite.c>
  DirectoryIndex index.php
  RewriteEngine on

  RewriteCond %{HTTP_HOST} !noredirect.waltertavares.pt$ [NC]
  RewriteCond %{HTTP_HOST} (.*).waltertavares.pt$ [NC]
  RewriteRule ^(.*)$ http://www.waltertavares.pt/$1 [L,R=301]
</IfModule>

Of course you can skip redirection on a full list of subdomains by duplicating the line

1
2
3
  RewriteCond %{HTTP_HOST} !noredirect.waltertavares.pt$ [NC]
  RewriteCond %{HTTP_HOST} !no-redirect.waltertavares.pt$ [NC]
  RewriteCond %{HTTP_HOST} !real-sub-domain.waltertavares.pt$ [NC]

Unix bash aliases

Well, I’m a little bored by, every time I change my system I have to update my aliases. So here it goes some alias that can be really useful. I gonna use this page to save all my aliases.

Both systems .bash_aliases:

1
2
3
4
alias duh ='du -h -d1'
alias ll  ='ls -lA --color'
alias l   ='ls -FhsSA1'
alias gpa ='for r in $(git remote); do git push $r master; done';

Linux .bash_aliases:

1
alias print ='echo -e'

MacOSX .bash_aliases:

1
alias print ='echo'

Linux .bash_colors:

1
2
3
4
RED   ='e[1;31m'
CYAN  ='e[1;36m'
GREEN ='e[1;32m'
NC    ='e[0m' # No Color

MacOSX .bash_colors:

1
2
3
4
RED   ='x1B[1;31m'
CYAN  ='x1B[1;36m'
GREEN ='x1B[1;32m'
NC    ='x1B[0m' # No Color