I
I
Ivan Antonov2016-03-12 02:09:15
symfony
Ivan Antonov, 2016-03-12 02:09:15

How to get routes from database?

The project has a categories table that contains the entire site structure with links (in the alias field) and metadata. The called controller is specified in the action field.
How can I load all routes from this table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Antonov, 2016-03-13
@antonowano

I did this:
in app/config/routing.yml

# It must be the last
category:
    path:       "/{alias}"
    defaults:  { _controller: "AppBundle:Default:category" }
    requirements:
        alias:  "[a-z_-]+(\/[a-z_-]+)*"

in DefaultController:
/**
     * @ParamConverter("category", class="AppBundle:Category")
     */
    public function categoryAction(Category $category, Request $request)
    {
        $id = $request->query->getInt('id');
        if (null == $id) {
            return $this->run('index', $category, $request); // indexAction
        } else {
            return $this->run('details', $category, $id); // detailsAction
        }
    }

    private function run($actionName, $category)
    {
        $params = func_get_args();
        array_shift($params);
        $namespace = "\\AppBundle\\Controller\\". $category->getController() . 'Controller';
        $actionName .= 'Action';
        /** @var Controller $controller */
        $controller = new $namespace();
        $controller->setContainer($this->container);
        return call_user_func_array(array($controller, $actionName), $params);
    }

In a DB I specify only a controller name. $category->getController()just returns it.

M
Michael, 2016-03-13
@maninhat

Have a look at symfony.com/doc/current/cmf/bundles/routing/introd... . The bundle allows you to connect your own router, which can take data from anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question