Url usage

Adding custom url capabilities to your resource

1. Create your service

You have to create (or extend) a service that implements Loginet\UrlBundle\Model\UrlProvider\UrlProviderInterface

# Loginet\DemoBundle\Model\DemoUrlModule\DemoUrlResource.php
<?php
namespace Loginet\DemoBundle\Model\DemoUrlModule;

use Loginet\UrlBundle\Model\UrlProvider\UrlProviderInterface;
use Loginet\UrlBundle\Model\Url\Url;

class DemoUrlResource implements UrlProviderInterface
{
    public function getControllerName(Url $url)
    {
        // return FQCN to your resources controller action
        return 'LoginetDemoBundle:Resource:showResource';
    }

    public function getPathParams(Url $url)
    {
        // return the necessary path paramaters for your controller
        return [
            'somePathVariable' => 'something',
        ];
    }

    public function getQueryParams(Url $url)
    {
        // return the necessary query paramaters for your controller
        return [];
    }
}

2. Tagging your service

You have to tag the service created in step 1. with loginet.url.provider

# Loginet\DemoBundle\Resources\config\services.yml
services:
    loginet.demo.url_resource:
        class: Loginet\DemoBundle\Model\DemoUrlModule\DemoUrlResource
        arguments: []
        tags:
            - {name: loginet.url.provider, alias: demoResource }

You must define a unique alias for your URL provider, you will be using it later.

3. Adding url editor to the admin

You have to use the urlEditor directive in your resource edit page.

# demoResource-edit.html
    ...
    <url-editor resource="data" resource-type="demoResource"></url-editor>
    ...
  • resource argument: An object variable in your current $scope, MUST HAVE an id field. The id field will be used to identify your resource.
  • resource-type argument: Your url providers alias

When you save your content you must make a content-saved broadcast. This will cause the url editor to save your data.

# demoResourceController.js
$scope.save = function(){
    // Save your resource
    $scope.$broadcast('content-saved');
}

4. Modifying your controllers

The controller action you given at step 1. must be modified if you want your default url redirecting to the custom url set up for the resource.

# Loginet\DemoBundle\Controller\DemoResourceController.php
<?php
namespace Loginet\DemoBundle\Controller;

use Loginet\UrlBundle\Model\Url\UrlManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class DemoResourceController extends Controller
{

    function showDemoResourceAction(Request $request)
    {
        // ... Get your resource in some way
        if (!$this->get('request_stack')->getParentRequest()) {
            $um = $this->getUrlManager();
            try {
                $latest = $um->findByResource('demoResource', $resourceId, 'hu');
                return $this->redirectToRoute('loginet_url_catch_all', array_merge(['url' => $latest->getUrl()], $request->query->all()), Response::HTTP_MOVED_PERMANENTLY);
            } catch (NotFoundHttpException $e) {
                //noop
            }
        }
        // ...
    }

    /**
     * @return UrlManager
     */
    public function getUrlManager()
    {
        return $this->get('loginet.url.manager');
    }
}

You must only check for custom url-s if the request is the master request as the url manager will be calling this action in a sub request.