Giter VIP home page Giter VIP logo

uniqueslugbundle's Introduction

FbeenUniqueSlugBundle

With this bundle you will be able to automatic generate unique slugs inside your entities by simply add a @Slug annotation to a specified field.

Features include:

  • very simple installation
  • generate unique slugs from one or more properties in the same entity
  • automatic storage into the database
  • making the slug unique by adding a digit when needed
  • add or check slugs in existing tables with the command prompt
  • support for date, time and datetime fields with custom format

Installation

Using composer:

  1. Add "fbeen/uniqueslugbundle": "dev-master" to the require section of your composer.json project file.
    "require": {
        ...
        "fbeen/uniqueslugbundle": "dev-master"
    },
  1. run composer update:

    $ composer update

  2. Add the bundle to the app/AppKernel.php:

        $bundles = array(
            ...
            new Fbeen\UniqueSlugBundle\FbeenUniqueSlugBundle(),
        );

Adding Slug behavior to your entity

Suppose that you have a "Newsitem" entity to display some news on your website. You might have a entity like this:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Fbeen\UniqueSlugBundle\Annotation\Slug;

/**
 * Newsitem
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Newsitem
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=245)
     */
    private $title;

    /**
     * @var string
     * 
     * @Slug("title")
     *
     * @ORM\Column(name="slug", type="string", length=255, unique=true)
     */
    private $slug;

    /**
     * @var string
     *
     * @ORM\Column(name="body", type="text")
     */
    private $body;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime")
     */
    private $created;

    public function __construct()
    {
        $this->created = new \DateTime();
    }
}

Important notes about this example:

  • Do not forget the use Fbeen\UniqueSlugBundle\Annotation\Slug;

  • Add a @Slug("title") annotation to the slug property to tell the application it should create a slug from the $title property

  • Add a constructor that sets the $created property to the current date and time

  • use the following commands on the console to add the getters and setters and to update the database:

    $ php app/console doctrine:generate:entities AppBundle:Newsitem

    $ php app/console doctrine:schema:update --force

Using the Slugs in your routes

From now on if you persist your entity the slug will be automatically generated. To use the slugs into the routes you could simply use the $slug property into the route e.g. @Route("news/{slug}", name="newsitem_show")

And then you have to retrieve the right newsitem using the given slug:

    /**
     * Finds and displays a Newsitem entity.
     *
     * @Route("/{slug}", name="newsitem_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($slug)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('AppBundle:Newsitem')->findOneBy(array('slug' => $slug));

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Newsitem entity.');
        }

        return array(
            'entity'      => $entity,
        );
    }

Advanced Slugs

The slug annotation has some more futures:

  1. To generate slugs from more than one property just write an array of properties: @Slug({"created", "title"})

  2. To add your own format for date, time and datetime fields use the format parameter: @Slug({"created", "title"}, format="Y-m-d").

Update the slugs for all records of your table

Using the Symfony console commands you are able to generate the slugs for all the records:

$ php app/console fbeen:generate:slugs

You will have to type the entity shortcut and then you need to confirm the update.

Important notes

  1. The slugs will be truncated to the length of the slug field minus 10 characters! the last 10 positions are used for additional digits that will make the slug unique if necessary.

uniqueslugbundle's People

Contributors

fbeen avatar pedrosoft avatar

Watchers

James Cloos avatar Petronel MALUTAN (p.m) avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.