Giter VIP home page Giter VIP logo

t3ext-routing's Introduction

Request Routing Service

Service to route HTTP/REST requests to your own controller/actions.

Good to know: The routing is inspired by the way Flow's router works (read more).

What does it do?

This extension lets you route requests like:

http://localhost/routing/extension-key/my-demo/1234
http://localhost/routing/extension-key/my-demo/1234.json
http://localhost/routing/extension-key/my-demo/99

to any controller/action based on a YAML-based routing configuration. In this example, where 1234 and 99 will be mapped to some method argument (and converted to domain object if needed) and json will set the response format to json.

Sample Routing

The routing is stored as Configuration/Routes.yaml (or Configuration/Routes.yml) and looks like that

-
  name: 'Demo action with a parameter in a given format (JSON, ...)'
  uriPattern: 'my-demo/{value}.{@format}'
  defaults:
    '@package':    'MyVendor.ExtensionKey'
    '@plugin':     'API'
    '@controller': 'Dummy'
    '@action':     'demo'
-
  name: 'Demo action with a parameter'
  uriPattern: 'my-demo/{value}'
  defaults:
    '@package':    'MyVendor.ExtensionKey'
    '@plugin':     'API'
    '@controller': 'Dummy'
    '@action':     'demo'

The name of the route is sent as additional header in the response:

Additional HTTP header "X-Causal-Routing-Route"

Installation

  1. Install extension with packagist:

    composer req causal/routing
    
  2. Go to Extension Manager and activate extension routing

  3. Add a rewrite rule to your .htaccess:

    RewriteRule ^routing/(.*)$ /index.php?eID=routing&route=$1 [QSA,L]
    

    or, if you are using Nginx:

    rewrite ^/routing/(.*)$ /index.php?eID=routing&route=$1 last;
    

This will have the effect of using this extension for handling requests starting with routing/.

Tip: If you need to support localization (&L=<some-language-uid>), then you should change the suggesting routing above to include the root page uid of your website (&id=<some-uid>). This is needed because localization mode and default language may differ in complex environments and thus cannot be inferred.

Read more in the manual.

Compatibility with TYPO3 v8

Since change 78002, you need to adapt your TypoScript so that cHash is not required:

plugin.tx_<yourext> {
    features {
        requireCHashArgumentForActionArguments = 0
    }
}

t3ext-routing's People

Contributors

alexkvrlp avatar somebdyelse avatar tantegerda1 avatar xperseguers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

t3ext-routing's Issues

TYPO3 CMS 7.2.0-dev, routing is not working

In the last 24h something occurs with TYPO3 core and then the new EidRequestHandler does indeed not set a TT object.

I' getting this output:

( ! ) Fatal error: Call to a member function push() on null in /var/www/typo3_src/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php on line 1206
Call Stack

Time Memory Function Location

1 0.0719 240072 {main}( ) .../index.php:0
2 0.0790 639632 TYPO3\CMS\Core\Core\Bootstrap->run( ??? ) .../index.php:33
3 0.1116 2472944 TYPO3\CMS\Frontend\EidRequestHandler->handleRequest( ) .../Bootstrap.php:195
4 0.1249 2482888 require( '/var/www/typo3conf/ext/routing/Classes/Controller/RoutingController.php' ) .../EidRequestHandler.php:61
5 0.2003 2609712 Causal\Routing\Controller\RoutingController->dispatch( ) .../RoutingController.php:332
6 0.2108 2650536 Causal\Routing\Controller\RoutingController->initTSFE( ) .../RoutingController.php:114
7 0.8588 4934936 TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->determineId( ) .../RoutingController.php:314

JsonView seems not to work

Either I am doing something wrong or the export via JsonView seems not to work. Each time I call my route the request returned a response with the content-type defined as text/html; charset=UTF-8 instead of application/json.

URLs to browse

http://app.dev/routing/block/api/list

or

http://app.dev/routing/block/api/list.json

Configuration/Routes.yaml

-
  name: 'Demo action'
  uriPattern: 'api/{@action}'
  defaults:
    '@package': 'VENDOR.Block'
    '@plugin': 'Blockplugin'
    '@controller': 'Block'
    '@format': 'json'

or

-
  name: 'Demo action'
  uriPattern: 'api/{@action}.{@format}'
  defaults:
    '@package': 'VENDOR.Block'
    '@plugin': 'Blockplugin'
    '@controller': 'Block'

Both configurations and URLs are not working.

Classes/Controller/BlockController.php

<?php
namespace VENDOR\Block\Controller;

class BlockController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
    /**
     * @var \VENDOR\Block\Domain\Repository\BlockRepository
     * @inject
     */
    protected $blockRepository = NULL;

    /**
     * action list
     */
    public function listAction() {
        $blocks = $this->blockRepository->findByUid(1);
        $this->view->assign('blocks', $blocks);
        $this->view->setVariablesToRender(['blocks']);
    }
}

Classes/View/Block/ListJson.php

<?php
namespace VENDOR\Block\View\Block;

class ListJson
    extends \TYPO3\CMS\Extbase\Mvc\View\JsonView
{
    protected $configuration = array(
        'blocks' => [
            '_descendAll' => []
        ]
    );
}

Output

<html><head><style type="text/css"></style></head><body>{...}</body></html>

Route with {@controller}

Function "GeneralUtility::underscoredToUpperCamelCase" is not used on "$controllerParameters['@controller']" in "RoutingController.php".

Line 189:
$pluginParameters['controller'] = $controllerParameters['@controller'];
=> $pluginParameters['controller'] = GeneralUtility::underscoredToUpperCamelCase($controllerParameters['@controller']);

Line 192:
$_POST['controller'] = $controllerParameters['@controller'];
=> $_POST['controller'] = GeneralUtility::underscoredToUpperCamelCase($controllerParameters['@controller']);

JsonView documentation

I think it would be interesting to add a hint in "Using JsonView" bloc:
Json format must be specified in URI.

Cache handling

Could it be an idea to handle a 1st level cache directly in this extension ?
If the extension calls many other extensions with JSON-Views for example, the caching mechanism must be implemented in each of those extensions.

Pagetree routing

Hi Xavier,

I couldn't figure out if the extension also creates routes for the page tree. If not, it would be an awesome enhancement. It then will be a good choice over RealURL.

Regards

Compatibility TYPO3 CMS 8

Hi Xavier,

Quick question: is it anything in the pipeline regarding TYPO3 CMS 8 compatibility? Is it a lot to undertake to achieve that?

Greetings,

Fabien

Issue with causal/routing dev-master

Hi,

when we integrate the causal/routing dev-master on Typo3 8.7.27 through composer,
The website is giving us a 500 error.
When we checked the log file we have the below error

PHP Fatal error: Uncaught BadFunctionCallException: TYPO3 Fatal Error: Extension key "core" is NOT loaded!

Are we missing any module that is required for it?

Ensure code follows PSR-2 coding guidelines

As of version 0.5.0-dev, the code does not comply with PSR-2 coding guidelines. Most of the issues should be easy to fix, but I struggle to come up with an idea regarding the additional PHP code at the end of file RoutingController.php.

FILE: t3ext-routing/Classes/ViewHelpers/UriViewHelper.php
------------------------------------------------------------------------------------------
  41 | ERROR   | The closing brace for the class must go on the next line after the body
------------------------------------------------------------------------------------------

FILE: t3ext-routing/Classes/Controller/RoutingController.php
------------------------------------------------------------------------------------------
   1 | WARNING | A file should declare new symbols (classes, functions, constants, etc.)
     |         | and cause no other side effects, or it should execute logic with side
     |         | effects, but should not do both. The first symbol is defined on line 28
     |         | and the first side effect is on line 335.
 161 | WARNING | Line exceeds 120 characters; contains 135 characters
 179 | WARNING | Line exceeds 120 characters; contains 122 characters
 181 | WARNING | Line exceeds 120 characters; contains 122 characters
 191 | WARNING | Line exceeds 120 characters; contains 154 characters
 201 | WARNING | Line exceeds 120 characters; contains 148 characters
 204 | WARNING | Line exceeds 120 characters; contains 137 characters
 332 | ERROR   | The closing brace for the class must go on the next line after the body
------------------------------------------------------------------------------------------

FILE: t3ext-routing/ext_localconf.php
------------------------------------------------------------------------------------------
   6 | WARNING | Line exceeds 120 characters; contains 129 characters
------------------------------------------------------------------------------------------

The report above does not include file Classes/Library/Spyc/Spyc.php due to the fact that this is a 3rd party library.

Route explorer

It could be interesting to have an "explorer" output, to see available routes and arguments.

Alternate end point

Advanced option to change the default eID=routing with something else (could be useful when migrating from home-made REST dispatcher).

Bug Dispatch Controller (PUT, DELETE)

With HTTP PUT or DELETE method, dispatch to the controller doesn't work, in my case it is an action of the third controller define in "ext_localconf.php", from error the system is searching action in the first controller. Effectively it works with an action of the first controller define in "ext_localconf.php".
With POST method it works fine.
With a debug on "RoutingController.php" the line code "$bootstrap->run('', $configuration)" returns nothing.

Cache routes

And add a production context switch so that when not in production, routes are not cached

New tag/version with PHP 7 support

Hi @xperseguers

Could you please create a current version of this extension with PHP 7 support. I don't know if there are any issues regarding PHP 7 pending.

Otherwise it would be nice to pull the current master as version 0.4.0 ? ;-)

Best,
Jonas

CamelCase controller names are wrong converted

If you configure a UpperCamelCase controller name like this:
name: 'Routes for list of all userGroups'
uriPattern: 'userGroups'
defaults:
'@Vendor': 'FOO'
'@Package': 'Bar'
'@plugin': 'myplugin'
'@controller': 'UserGroup'
'@action': 'list'

the controller name is converted to Usergroup, due to the GeneralUtility::underscoredToUpperCamelCase conversion in RoutingController::getControllerParameters

I don't see why the underscoredToUpperCamelCase conversion is needed at all, since there shouldn't be underscores in class names in Extbase...?

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.