Git Product home page Git Product logo

modxfastrouter's Introduction

english | русский


MODX Fast Router

Key features

Usage

Routes must be declared in the chunk fastrouter in the following format:

[
    ["GET","/fastrouter/{name}/{id:[0-9]+}","1"],
    ["GET","/fastrouter/{id:[0-9]+}","1"],
    ["GET","/hello/{name}","1"],
    ["GET","/contact","1"],
    ["GET","/maybe-ajax-request","snippet_for_ajax_request"]
]
  • First argument request method (GET), can be one of GET, POST, PATCH, PUT, DELETE.
  • Second argument route path (/fastrouter/{name}/{id:[0-9]+}, where {name} named param with any character, {id:[0-9]+} must be a number). Allowed using regular expressions.
  • The third parameter is the resource ID (2) where the request will be sent, or the name of the snippet (snippet_for_ajax_request).

If the requested URL does not match any declared routes, a 404 error will be generated.

To prevent request from a recursion when using the sendErrorPage method in your snippets and components, you must pass array('stop' => true) to the method.
It should look like this:

$modx->sendErrorPage(array('stop' => true));

If you use the resource ID as a handler, then all named parameters will be available in the $_REQUEST array associates by fastrouter key.
In our case, the first route, eg http://site.com/fastrouter/vanchelo/10 we get the following data:

var_dump($_REQUEST);

// Array
// (
//     [q] => user/vanchelo/10
//     [fastrouter] => Array
//         (
//             [name] => vanchelo
//             [id] => 10
//         )
// )

If the handler is a snippet, all parameters will be available in $scriptProperties, you can get them this way:

$key = $modx->getOption('fastrouter.paramsKey', null, 'fastrouter');
$params = $modx->getOption($key, $scriptProperties, array());

return '<pre>' . print_r($params, true) . '</pre>';

By default, the key of request params is fastrouter. To define custom key, change fastrouter.paramsKey in system settings as you need.

Download the latest release from: https://github.com/vanchelo/modxFastRouter/releases.

Got questions?

If you have questions or general suggestions, don't hesitate to submit a new Github issue.

Contributing

  1. Fork it (https://github.com/vanchelo/modxFastRouter/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

License and Copyright

This software released under the terms of the MIT license.

modxfastrouter's People

Contributors

russelgal avatar vanchelo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

modxfastrouter's Issues

Подскажите пожалуйста пример сниппета

Не совсем понимаю почему если id не совпадает выкидывает 500ую ошибку?!

$key = $modx->getOption('fastrouter.paramsKey', null, 'fastrouter');
$params = $modx->getOption($key, $scriptProperties, isset($_REQUEST[$key]) ? $_REQUEST[$key] : []);

$res = $modx->getObject('sVideosVideo',$params);
if($params == $res->get('id')) {
    $output = $res->toArray();
    print "<pre>";
    print_r($output);
} else {
    return '<pre>' . print_r($params, true) . '</pre>';
}

Вот что получается. Там где id совпадают всё распечатывается как надо. Есть также вопрос как динамической странице присвоить шаблон?

Может стоит добавить префикс к переменным?

Может стоит добавить префикс к переменным? Например:

var_dump($_REQUEST);

Array
(
    [q] => user/vanchelo/10
    [route.name] => vanchelo
    [route.id] => 10
)

Или вынести переменные в отдельный массив:

var_dump($_REQUEST);

Array
(
    [q] => user/vanchelo/10
    [fastrouter] => Array
        (
            [name] => vanchelo
            [id] => 10
        )
)

Кто что думает?

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.