老兵的自主空间 2014-06-05
Symfony(1)Installation and Tour
1. Install symfoy
Check the PHP version on my machine
>php --version
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
That is not the latest stable version. I plan to update that. Check the latest version here
http://us2.php.net/downloads.php
>sudo rm -fr /usr/bin/php
>wget http://us1.php.net/distributions/php-5.5.13.tar.gz
Unzip that file
>./configure --prefix=/Users/carl/tool/php-5.5.13
>make
>make install
Then add the bin directory to the working environment
>php --version
PHP 5.5.13 (cli) (built: May 30 2014 18:07:00) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
Before we install Symfony, we first need composer.
https://getcomposer.org/
>curl -sS https://getcomposer.org/installer | php
When I do this, I got Error Information
#!/usr/bin/env php Some settings on your machine make Composer unable to work properly.Make sure that you fix the issues listed below and run this script again:The openssl extension is missing, which means that secure HTTPS transfers are impossible.If possible you should enable it or recompile php with --with-openssl
Solution:
Reinstall the php with openssl.
>./configure --prefix=/Users/carl/tool/php-5.5.13 --with-openssl
Error Message when I execute the command make
Undefined symbols for architecture x86_64:
Solution:
http://www.devsumo.com/technotes/2013/02/building-php-on-os-x/
>export LDFLAGS=-lresolv
>make clean
>make
Still not working, Error Message
Undefined symbols for architecture x86_64: "_iconv_close", referenced from: _zif_iconv_substr in iconv.o _zif_iconv_mime_encode in iconv.o _php_iconv_string in iconv.o __php_iconv_strlen in iconv.o __php_iconv_strpos in iconv.o __php_iconv_mime_decode in iconv.o _php_iconv_stream_filter_cleanup in iconv.o ... "_iconv_open", referenced from: _zif_iconv_substr in iconv.o _zif_iconv_mime_encode in iconv.o _php_iconv_string in iconv.o __php_iconv_strlen in iconv.o __php_iconv_strpos in iconv.o __php_iconv_mime_decode in iconv.o _php_iconv_stream_filter_factory_create in iconv.o ... ld: symbol(s) not found for architecture x86_64
Solution:
http://superuser.com/questions/394219/compiling-php-on-os-x-iconv-works-only-if-forced-to-64-bit
http://miphol.com/muse/2013/08/libiconv-on-mac-os-x-1084.html
http://stackoverflow.com/questions/5835847/libiconv-2-dylib-mac-os-x-problem
Backup and delete the related files under /opt/local/lib
>cd /opt/local/lib
>sudo mv libiconv.* /Users/carl/data/baklib/
>cd /Users/carl/data/baklib
>ls
-rw-r--r-- 1 root admin 1048064 Nov 18 2013 libiconv.2.dylib -rw-r--r-- 1 root admin 1096256 Nov 18 2013 libiconv.a lrwxr-xr-x 1 root admin 16 Nov 18 2013 libiconv.dylib -> libiconv.2.dylib
>./configure --prefix=/Users/carl/tool/php-5.5.13 --with-openssl --with-iconv-dir=/usr/lib
>make
>make install
After the installation, I will copy my backup libs back to /opt/local/lib, then go on with composer
>curl -sS https://getcomposer.org/installer | php
>mkdir /Users/carl/tool/composer/bin
>mv composer.phar /Users/carl/tool/composer/bin/composer
Check my installation is fine.
>composer --version
Composer version f16e3a88e28228af67cc98950a2b621713f4aac3 2014-06-03 08:46:14
2. Create my First Project
>composer create-project symfony/framework-standard-edition myfirstproject/ ~2.4
Then it will generate the project for me.
>cd myfirstproject/
This is the first time for me to run PHP/Symfony project here on my machine. So I will check the requirements.
>php app/check.php
Information
* WARNING: No configuration file (php.ini) used by PHP!
ERROR date.timezone setting must be set Set the "date.timezone" setting in php.ini* (like Europe/Paris).
WARNING mb_strlen() should be available Install and enable the mbstring extension.
WARNING intl extension should be available Install and enable the intl extension (used for validators).
WARNING a PHP accelerator should be installed Install and enable a PHP accelerator like APC (highly recommended).
WARNING short_open_tag should be disabled in php.ini Set short_open_tag to off in php.ini*.
Solution:
http://www.inmotionhosting.com/support/website/php/setting-the-timezone-for-php-in-the-phpini-file
http://php.net/manual/en/timezones.others.php
http://www.php.net/manual/en/timezones.php
>sudo vi /etc/php.ini
Search for date.timezone
Change the content as follow
date.timezone = "US/Central"
Ok, it does not working, that is only because I am changing the wrong php.ini
>php --ini
Configuration File (php.ini) Path: /Users/carl/tool/php-5.5.13/lib
So go and change the file in that place.
>sudo rm -fr /etc/php.ini
>cp /etc/php.ini.default /opt/php/lib/php.ini
>sudo ln -s /opt/php/lib/php.ini /etc/php.ini
And change the timezone as follow>
date.timezone = America/North_Dakota/Center
Run the first project
>php app/console server:run
Visit the page
http://127.0.0.1:8000/
Error Message:
No route found for "GET /"
Solution:
When I create the project I should use this command.
>composer create-project symfony/framework-standard-edition myfirstproject ~2.4.6
3. Understand the Tour
Hello World Demo is here http://localhost:8000/demo/hello/World
Routing
ProjectName/app/config/routing_dev.yml
ProjectName/src/Acme/DemoBundle/Resources/Config/routing.xml
Controllers
Instead of using PHP global variables and functions($_GET, headers()), Symfony uses objects: Request and Response.
Magic things, PHP annotation, actually, it is JAVA like codes. And PHP also has the idea of Controller.
ProjectName/src/Acme/DemoBundle/Controller/WelcomeController.php
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller
…snip...
Bundle under vendor and src
public function indexAction()
{
return $this->render('AcmeDemoBundle:Welcome:index.html.twig');
}
And we can put more stuff in response like this
public function indexAction(){
$response = $this->render(‘AcmeDemoBundle:Welcome:index.txt.twig’);
$response->headers->set(‘Content-Type’, ‘text/plain’);
return $response;
}
AcmeDemoBundle:Welcome:index.html.twig is the logic name, we found the page here
ProjectName/src/Acme/DemoBundle/Resources/views/Welcome/index.html.twig
And in the routing.yml, the routing defined as follow:
_demo_secured:
resource: "@AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
Now we will see, it is said annotation, and the annotation codes are as follow:
/**
* @Route("/hello/{name}", name="_demo_hello")
* @Template()
*/
Templates
Symfony2 uses Twig as its template engine. The template file is just text file, the extension name is not important, .html .htm .twig.
{% … %} is for the control statement.
{{ … }} is for the content statement.
IDE Support for Sublime Text https://github.com/Anomareh/PHP-Twig.tmbundle
References——> Package Control ——> Install Package ——>PHP-Twig
Flow Control
{% for user in users %}
{{ user.username }
{% endfor %}
{% if users|length > 0 %}
…
{% endif %} {# here is the comments #}
Load Other Template
{% include ’sidebar.html’ %}
Even sometimes, the template name can be variables.
{% for box in boxes %}
{% include “render_box.html” %}
{% endfor %}
Bundle - a set of files(PHP files, stylesheets, JavaScripts, images, ...)
Environment - dev and prod
References:
http://symfony.com/
http://symfony.com/get-started
http://symfony.com/doc/current/quick_tour/the_big_picture.html
PHP envirenment
http://sillycat.iteye.com/blog/562652
http://tutorial.symblog.co.uk/
https://github.com/dsyph3r/symblog
https://github.com/Monomachus/FirstSymfony2App
https://github.com/symfony/symfony-standard
Twig template
http://blog.csdn.net/jiaochangyun/article/details/7180758