!
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- .
- .
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
Id |
Name |
Description |
Seller |
Publishdate |
Actions |
{% for product in products %}
{{ product.id }} |
{{ product.name }} |
{{ product.description }} |
{{ product.seller }} |
{% if product.publishDate %}{{ product.publishDate|date('Y-m-d H:i:s') }}{% endif %} |
|
{% endfor %}
{% 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")
:
. , .
Symfony ,
.
, 3d . , .
!
https://habrahabr.ru/post/330170/