Have you ever wanted to get the name of the current controller inside one of your Symfony2 Twig files? Well good news, I can show you how.

Home

I am sure there are some other good parameters inside the _template variable too!

If you followed my previous post to get mopa bootstrap working with assetic, you might be running into a problem like the error below.

After some digging around I found this which points to a forum post that tells us to comment out the two un-commented lines in /Applications/MAMP/Library/bin/envvars. Give that a shot and see if it works for you too!

[exception] 500 | Internal Server Error | RuntimeException
[message] The process stopped because of a "0" signal.
[1] RuntimeException: The process stopped because of a "0" signal.
                at n/a
                    in .../vendor/symfony/symfony/src/Symfony/Component/Process/Process.php line 375

                at SymfonyComponentProcessProcess->wait(null)
                    in .../vendor/symfony/symfony/src/Symfony/Component/Process/Process.php line 175

                at SymfonyComponentProcessProcess->run()
                    in /Users/mikeosterhout/Sites/builditawesome-sandbox/vendor/kriswallsmith/assetic/src/Assetic/Filter/LessFilter.php line 123

                at AsseticFilterLessFilter->filterLoad(object(FileAsset))
                    in /Users/mikeosterhout/Sites/builditawesome-sandbox/vendor/kriswallsmith/assetic/src/Assetic/Filter/FilterCollection.php line 62

                at AsseticFilterFilterCollection->filterLoad(object(FileAsset))
                    in /Users/mikeosterhout/Sites/builditawesome-sandbox/vendor/kriswallsmith/assetic/src/Assetic/Asset/BaseAsset.php line 90

                at AsseticAssetBaseAsset->doLoad('
@import "../bootstrap/less/bootstrap.less";

I have been using Symfony2 a lot lately and I recently had the need to use the Country Form Type. When I tried to use the form type $builder->add('country', 'country'); I was getting the following error:

Fatal error: Class 'ResourceBundle' not found in .../vendor/symfony/symfony/src/Symfony/Component/Locale/Locale.php on line 51

This error is not very helpful but I can tell you that it is a result of not having the intl extension enabled. Here is how you can get it set up.

1. Get the intl.so file

I found this to be easier than this, but it does require that you install mac ports and you will have a opt folder left over on your computer when you are done.

2. Place the intl.so file in the extension folder

For php 5.3.14 that would be this folder /Applications/MAMP/bin/php/php5.3.14/lib/php/extensions/no-debug-non-zts-20090626

If you are using a different version of php 5.3 you should just have to change that in the path. I am still looking into how this would be handled with php 5.4, if anyone knows please post in the comments.

3. Enable the intl extension in php.ini

You just need to add extension=intl.so someplace in your php.in file

4. Done!

However, with php 5.4 already here we will have to revisit this post soon.

The other day I wanted to integrate Bootstrap into a Symfony2 project. I came across a nice bundle that helps with this called MopaBootrapBundle.

Here are the steps I took to get it working.

We need node and less, so I used brew to get those installed.

0. Install Xcode and brew if you have not done so already. We will use brew to install node and brew relies on Xcode.

1. install node

brew install node

2. now install node package manager(npm) to help us with the less install

curl http://npmjs.org/install.sh | sh

3. use npm to install less module

npm install -g less

Back to symfony...

4. set up Assetic to use less, my paths are for a default install on OSX

filters:
        cssrewrite: ~
        less:
            node:       /usr/local/bin/node
            node_paths: [/usr/local/lib/node, /usr/local/lib/node_modules]
            apply_to: ".less$"

5. Install MopaBootstrapBundle and Bootstrap via composer.json

"require": {
        "mopa/bootstrap-bundle": "dev-master",
        "twitter/bootstrap": "master"
},
..
..
"repositories": [
       {
           "type": "package",
           "package": {
               "version": "master",
               "name": "twitter/bootstrap",
               "source": {
                   "url": "https://github.com/twitter/bootstrap.git",
                   "type": "git",
                   "reference": "master"
               },
               "dist": {
                   "url": "https://github.com/twitter/bootstrap/zipball/master",
                   "type": "zip"
               }
           }
       }
   ],
..
..
"post-install-cmd": [
   "Mopa\Bundle\BootstrapBundle\Composer\ScriptHandler::postInstallSymlinkTwitterBootstrap"
],
"post-update-cmd": [
   "Mopa\Bundle\BootstrapBundle\Composer\ScriptHandler::postInstallSymlinkTwitterBootstrap"
]

6.Add MopaBootrapBundle to your AppKernel.php

new MopaBundleBootstrapBundleMopaBootstrapBundle(),

7.Start using it! I used vendor/mopa/boostrap-bundle/Mopa/Bundle/BootstrapBundle/Resources/views/base.html.twig as a template for my own view file...

[edit] if you run into issues with symlinking check out the last couple posts on this thread http://www.princexml.com/bb/viewtopic.php?f=4&t=3204 - you will have to comment out the uncommented lines in the envvars file

Have you ever tried to create a link dynamically in php with a URL such as 'facebook.com'? If you have you know that URLs like that don't work so well. It will simply append the URL onto the end of the current page. So if the visitor is on a page like mysite.com/my-page they will get a link like mysite.com/my-pagefacebook.com.

This is kind of annoying, so I created a helper function that will add the http:// part if it is missing from the string you are trying to create a link for.

Check out the function below. It simply checks to see if the $url contains either http:// or https:// and if it doesn't it will append http:// to the end.

This function could probably be even better, but it is pretty useful as is. Feel free to suggest any improvements in the comments below.

function maybe_add_http($url){
	if(!preg_match("~^(?:ht)tps?://~i", $url))
		$url = "http://" . $url;
	return $url;
}

To use it simply pass your URL into the function and let it do the rest!