Custom lithium routing scenario

限于喜欢 提交于 2019-12-05 21:17:08

If you don't have any convention in the URL for what is what, between page, item and category. I'd go with a very generic router.

Router::connect('/{:category}/{:page}/{:item}', 'Pages::any');
Router::connect('/{:category}/{:page}', array('Pages::any', 'item' => null));
Router::connect('/{:category}', array('Pages::any', 'page' => null, 'item' => null));

And in Pages::any() to search for the correct stuff. Is that category a page after all (example 4)? Is that page an item (example 1)?

or

You store the URL somewhere (e.g. a mapping table in the database) and use the pattern version of a lithium Route.

Router::connect(new Route(array(
    'pattern' => '@^/(?<path>.+)$@',
    'params' => array('controller' => 'pages', 'action' => 'any'),
    'keys' => array('path' => 'path'),
    // extra stuff, if the path is `tata`, it skips this route and uses
    // any of the following ones that matches.
    'handler' => function($request) {
        if ($request->params['path'] == 'tata') {
            return false;
        } else {
            return $request;
        }
    }
)));

From that point, you'll get the full URL.

You probably should write a smart Router Helper which is maybe able to process your request based on your db defined routes.

Take a look into: net/http/Router.php

especially connect(), parse() and match()

I would start to write some kind of anonymous function and progress it to a testable Class which is located in /extension.. ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!