I want to play symfony 2, but I just start

Keywords: Programming PHP

introduce

First install a X.
Symfony is a component, a framework, a philosophy and a community - a perfect harmony
Symfony is not a bad choice - people who use Symfony have not yet been fired.

install

  1. Php-v I currently use PHP 7.1
  2. Composer installation
composer create-project symfony/framework-standard-edition your_project "2.7.*"
  1. start-up
cd your_project
php app/console server:run


You can't see anything like that. It's time to show the real technology and write the code.

Real technology (hello symfony2)

http://127.0.0.1:8000/lucky/hello

<?php
//-> src/AppBundle/Controller/IndexController.php
namespace AppBundle\Controller;


use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class IndexController extends Controller
{
    /**
     * @Route("/lucky/hello")
     */
    public function helloAction()
    {
        return new Response("hello symfony2");
    }
}

Result: Hello symfony 2
It's right to output. You can play foolishly. The frame can run, and it's happy to run.

Route

  1. Routing configuration
    Routing is simpler than annotation routing
# app/config/routing.yml
app:
    resource: '@AppBundle/Controller/'
    type: annotation
index_hello:
    path: /index/hello
    defaults: {_controller: AppBundle:Index:hello}
blog_list:
    path: /blog/{page}
    defaults: {_controller: AppBundle:Blog:list, page: 1}
    requirements: #Wildcard matching numbers
        page: '\d+'
blog_show:
    path: /articles/{_locale}/{year}/{slug}.{_format}
    defaults: {_controller: AppBundle:Blog:show, _format: html}
    requirements:
        _locale: en|fr
        _format: html|rss
        year: \d+
  1. Route generation
$this->generateUrl("blog_list", array('page'=>3))
//Base Class Controller Implementation
$this->container->get('router')->generate($route, $parameters, $referenceType);

GeneeUrl's third parameter use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

- UrlGeneratorInterface::ABSOLUTE_URL			http://example.com/dir/file
- UrlGeneratorInterface::ABSOLUTE_PATH			/dir/file
- UrlGeneratorInterface::RELATIVE_PATH			../parent-file
- UrlGeneratorInterface::NETWORK_PATH			//example.com/dir/file
//The generate() method receives an array of wildcard values to generate URIs. But if you pass in additional values, they will be added to the URI as query string s
// /blog/2?category=Symfony
$this->get('router')->generate('blog', array(
    'page' => 2,
    'category' => 'Symfony'
));
//twig
<script>
var route = "{{ path('blog_show', {'slug': 'my-blog-post'})|escape('js') }}";
</script>

Posted by aminnuto on Sun, 24 Mar 2019 05:51:28 -0700