-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


(SEF URLs) Symfony 3 slug,

, 05 2017 . 00:38 +
!

Symfony. , CRUD (scaffolding) Symfony, . , , , . . , Symfony . , ( ). , , ?




, .

URL. Friendly URL Semantic URL. : SEF URLs Search Engine Friendly URLs.

?

, URL- . , , ? . , .

SEO , . URL , . , SEO . , .

,

URL- , GET URL- .

:
http(s):///slug-/slug-/slug---
http(s):////slug--

Slug, :

Slug ( ) - - (URL) . , , slug id- URL- .



- .

URL- ,
rozetka.com.ua ( , ). . :

:
rozetka.com.ua/t_balls/c81265

, c81265 , , id .

:
rozetka.com.ua/t_balls

id-? ? (http://rozetka.com.ua/contacts/)?
. , . Symfony , .

- , :
rozetka.com.ua/category/t_balls

Donic Elit 1* 6 (618016): rozetka.com.ua/198578/p198578

. .

URL? . URL , :


:

t_balls slug
donic-elit-1-6-beliy slug

.

Symfony

Symfony. Symfony 3.3.0. , Symfony .

Symfony 3.3.0 phpunit, . composer.json :

composer.json
...
    "require-dev": {
        ...
        "phpunit/phpunit": "^6.2.1"
        ...
    },
...
    "config": {
        "platform": {
            "php": "7.0.15"
        },
        ...
    },
...

:

composer update

, :

php composer.phar update

AppBundle :

php bin/console doctrine:generate:entity --entity=AppBundle:Product --fields="name:string description:text seller:string publishDate:datetime slug:string(length=128 nullable=false unique=true)" -q

, slug. , null. , id-, slug-. Slug id .

CRUD AppBundle:Product, . :

php bin/console doctrine:database:create #  
php bin/console doctrine:schema:create #     

php bin/console doctrine:generate:crud --entity="AppBundle:Product" --route-prefix=products --with-write -n # CRUD 



php bin/console server:run localhost:2020

http://localhost:2020/products/ :



. Doctrine.

Doctrine

Doctrine? slug ? . : slug , slug-, , . . Doctrine:

-> How to use Doctrine Extensions

StofDoctrineExtensionsBundle, Doctrine. :

-> StofDoctrineExtensionsBundle

StofDoctrineExtensionsBundle:

composer require stof/doctrine-extensions-bundle

:

app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
        );
        // ...
    }
    // ...
}

Doctrine Sluggable behavior extension. StofDoctrineExtensionsBundle , :

app/config/config.yml
...
stof_doctrine_extensions:
    default_locale: en_US
    orm:
        default:
            sluggable : true
...

Sluggable behavior extension . , . :

-> Sluggable behavior extension for Doctrine 2

, . - , , Product:slug, slug , :

src/AppBundle/Entity/Product.php
...
use Gedmo\Mapping\Annotation as Gedmo;
...

/**
 * Product
 *
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
 */
class Product
{
...
     /**
     * @var string
     *
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(name="slug", type="string", length=128, nullable=false, unique=true)
     */
    private $slug;
...
}

@Gedmo\Slug(fields={"name"}), , slug name. , . , : @Gedmo\Slug(fields={"publishDate", "name"}).

. , slug Doctrine :

src/AppBundle/Form/ProductType.php
...
class ProductType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name')->add('description')->add('seller')->add('publishDate'); // ->add('slug')
    }
    ...
}

(http://localhost:2020/products/new)


, slug . :



.



. products_show products_edit:



, id-, slug-. products_delete , , .

src/AppBundle/Controller/ProductController.php
...
class ProductController extends Controller
{
     ...
     /**
     * Finds and displays a product entity.
     *
     * @Route("/{slug}", name="products_show")
     * @Method("GET")
     * @param string $slug
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function showAction(string $slug)
    {
        $product = $this->getDoctrine()
            ->getRepository('AppBundle:Product')
            ->findOneBySlug($slug);
        $deleteForm = $this->createDeleteForm($product);

        return $this->render('product/show.html.twig', array(
            'product' => $product,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing product entity.
     *
     * @Route("/{slug}/edit", name="products_edit")
     * @Method({"GET", "POST"})
     * @param Request $request
     * @param string $slug
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function editAction(Request $request, string $slug)
    {
        $product = $this->getDoctrine()
            ->getRepository('AppBundle:Product')
            ->findOneBySlug($slug);

        $deleteForm = $this->createDeleteForm($product);
        $editForm = $this->createForm('AppBundle\Form\ProductType', $product);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('products_edit', array('slug' => $product->getSlug()));
        }

        return $this->render('product/edit.html.twig', array(
            'product' => $product,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    ...
}

app/Resources/views/product/index.html.twig
{% extends 'base.html.twig' %}

{% block body %}
    

Products list

{% for product in products %} {% endfor %}
Id Name Description Seller Publishdate Actions
{{ product.id }} {{ product.name }} {{ product.description }} {{ product.seller }} {% if product.publishDate %}{{ product.publishDate|date('Y-m-d H:i:s') }}{% endif %}
{% endblock %}


app/Resources/views/product/show.html.twig
{% extends 'base.html.twig' %}

{% block body %}
    

Product

Id {{ product.id }}
Name {{ product.name }}
Description {{ product.description }}
Seller {{ product.seller }}
Publishdate {% if product.publishDate %}{{ product.publishDate|date('Y-m-d H:i:s') }}{% endif %}
Slug {{ product.slug }}
{% endblock %}

:



: @Route("/{slug}", name="products_show")

: @Route("/{slug}/edit", name="products_edit")



:

  • slug
  • slug id-
  • slug ,

. , .

Symfony , .

, 3d . , .

!
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330170/

:  

: [1] []
 

:
: 

: ( )

:

  URL