Coder Social home page Coder Social logo

routerbundle's Introduction

ONGR Router Bundle

Router Bundle allows to define and match URLs for elasticsearch documents. At url matching phase it additionaly searches for elasticsearch documents with specified url.

This can be used for generating/matching nice URLs for any document. Beautiful URLs help SEO and improve user's usability experience.

If you have any questions, don't hesitate to ask them on Join the chat at https://gitter.im/ongr-io/support chat, or just come to say Hi ;).

Build Status Coverage Status Latest Stable Version Scrutinizer Code Quality

Documentation

The online documentation of the bundle is here

For contribution rules take a look at contribute topic.

Setup the bundle

This bundle strongly uses ONGR Elasticsearch Bundle. We assume that you are familiar with it and it already suits your needs.

Step 1: Install Router bundle

Router bundle is installed using Composer.

composer require ongr/router-bundle "~1.0"

Enable Router and Elasticsearch bundles in your AppKernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(),
        new ONGR\RouterBundle\ONGRRouterBundle(),
    );
}

Yep, that's it, 1 step installation. All the next steps are demonstration how to setup product document with SEO URLs. So look below how easy it is and adapt it for your needs.

Step 2: Add configuration

Add minimal configuration for Router and Elasticsearch bundles.

#app/config/config.yml
ongr_elasticsearch:
    analysis:
        analyzer:
            urlAnalyzer:
                type: custom
                tokenizer: keyword
                filter: [lowercase]
    connections:
        default:
            index_name: acme
            analysis:
                analyzer:
                    - urlAnalyzer
    managers:
        default:
            connection: default
            mappings:
                - AppBundle

ongr_router:
    manager: es.manager.default
    seo_routes:
        product: AppBundle:Product:document
        # ...

WARNING: If SeoAwareTrait is used you must implement urlAnalyzer analyzer, otherwise there will be a fatal error on index create.

At _controller you define controller and action for every document type. _route is a name of this route and it can be used at path generation.

urlAnalyzer at ongr_elasticsearch configuration defines how all url fields are analyzed by Elasticsearch.

Check Elasticsearch bundle mappings docs for more information about the configuration.

Usage example

Step 1: Create a Product document

Lets create a Product document class. We assume that we have an AppBundle installed.

<?php
// src/AppBundle/Document/Product.php

namespace AppBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

/**
 * @ES\Document()
 */
class Product
{
    use DocumentTrait;
    use SeoAwareTrait; // <- Trait for URL's

    /**
     * @ES\Property(type="string")
     */
    public $title;

    // ...
}

In favor to support friendly URLs, the bundle provides SeoAwareTrait with predefined mapping.

Step 2: Create controller and action for the product page

<?php
// src/AppBundle/Controller/ProductController.php

namespace AppBundle\Controller;

use AppBundle\Document\Product;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class ProductController extends Controller
{
    public function documentAction(Product $document)
    {
        return new Response("Product: " . $document->title);
    }
}

Step 3: Create index and insert demo product

Create an elasticsearch index by running this command in your terminal:

    app/console ongr:es:index:create
    

More info about all commands can be found in the Elasticsearch bundle commands chapter.

Also, run the following curl command in your terminal to insert a product for this demonstration.

    curl -XPOST 'http://localhost:9200/acme/product?pretty=1' -d '{"title":"Acoustic Guitar", "url":"/music/electric-guitar"}'
    

Step 4: Check if it works

Just visit /music/electric-guitar page and you should see a title of the product that you inserted in step 3.

License

This bundle is under the MIT license. Please, see the complete license in the bundle LICENSE file.

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.