Symfony(4)Pages in Symfony2

Symfony(4)Pages in Symfony2

There is a simple two-step process.
Create a Route
Create a Controller

1. Environment and Front Controllers
There are 3 environment defined — dev, test and prod.

Symfony2 comes with 2 web-accessible front controller: app_dev.php and app.php.

When running in debug mode, Symfony2 runs slower, but all changes are reflected without having to manually clear the cache.

2. Build Simple Hello Page
A bundle is nothing more than a directory that houses everything related to a specific feature, including PHP classes, configuration, and even stylesheets and JavaScript files.

Create the Bundle
>php app/console generate:bundle --namespace=Acme/HelloBundle --format=yml

After that a directory(bundle) is created /src/Acme/HelloBundle, and one line is added to the file app/AppKernel.php.
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new Acme\HelloBundle\AcmeHelloBundle(),
        );

Create Router
The root router is already there in #app/config/routing.xml
helloBundle is already there
acme_hello:
    resource: "@AcmeHelloBundle/Resources/config/routing.yml"
    prefix:   /

in the sub routing.xml file src/Acme/HelloBundle/Resources/config/routing.yml
acme_hello_homepage:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeHelloBundle:Default:index }

Create the Controller
Based on the configuration, the src/Acme/HelloBundle/Controller/DefaultController.php will be called and the method should be indexAction
<?php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('AcmeHelloBundle:Default:index.html.twig', array('name' => $name));
    }
}

Then I visit the page http://sdk-consumer.local.sillycat.com/hello/good, it will give me the pages.

I am running that under apache. Here is another way to run it.
>php app/console cache:clear --env=prod --no-debug
>php app/console server:run --env=prod

Create The Template
Instead of writing the HTML inside the controller, render a template file instead.

return $this->render('AcmeHelloBundle:Default:index.html.twig', array('name' => $name));

BundleName:ControllerName:TemplateName
The physical location should be /path/to/BundleName/Resources/views/ControllerName/TemplateName

The twig file template will be as follow:
{# src/Acme/HelloBundle/Resources/views/Hello/index.html.twig #}
 {% extends '::base.html.twig' %}

 {% block body %}
     Hello {{ name }}!
 {% endblock %}

Pay attention to the ::base.html.twig, it is missing the BundleName:ControllerName, that means it is under the app/Resources

3. The Directory Structure
The Web Directory
Our front controller is there, app.php, app_dev.php. Actually our visit can be as follow:
http://sdk-consumer.local.sillycat.com/app_dev.php/hello/sillycat

Bundle Directory Structure
Controller/    — contains the controllers of the bundle
DependencyInjection/ — holds certain dependecy injection extension classes, for example, service configuration, compiler passes (optional)
Resources/config/ 
Resources/views/
Resources/public/ contains web assets(images, stylesheets, etc) and is copied or symbolically linked into the project web/ directory via the assets:install console command
Tests/

>php app/console assets:install


Tips
1. Permission Error when Execute composer update
Error Message:
  [ErrorException]                            touch(): Utime failed: Permission denied

Solution:
Find touch and change the rights
>which touch
/usr/bin/touch

>sudo chmod 755 /usr/bin/touch

Error Message:
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception                                                                     [RuntimeException]                                                           An error occurred when executing the "'cache:clear --no-warmup'" command.

Solution:
Run this command first, before we do composer update
>sudo rm -fr app/cache/*
>sudo rm -fr app/logs/*
>sudo rm -fr web/bundles/*

The root reason is that the apache is running with user daemon, my current user is carl. So I need to fix as follow:
>sudo chmod +a "daemon allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs web/bundles

>sudo chmod +a "carl allow delete,write,append,file_inherit,directory_inherit" app/cache app/logs web/bundles

References:
http://composer.golaravel.com/04-schema.html#extra   Configuration Doc for Composer
http://www.copernica.com/en/support/rest/example-get-post-put-and-delete-requests curl with PHP

http://kiwwito.com/unable-to-write-cache-file-problem-in-symfony-2/  Permission with composer and apache

你可能感兴趣的:(page)