Skip to content

[5.0] Add throwing InvalidArgumentException when undefined action passed to UrlGenerator::action() #7415

@MalikinSergey

Description

@MalikinSergey

If undefined action passed In UrlGenerator::action(), it's throw fatal exception

Call to a member function domain() on a non-object

with fully uninformative debug stack

because route, which was returned by $this->routes->getByAction($action) is not checked, before it passes to $this->toRoute(... and can be null instead of Route

    // current version
    public function action($action, $parameters = array(), $absolute = true)
    {
        if ($this->rootNamespace && ! (strpos($action, '\\') === 0))
        {
            $action = $this->rootNamespace.'\\'.$action;
        }
        else
        {
            $action = trim($action, '\\');
        }
        return $this->toRoute($this->routes->getByAction($action), $parameters, $absolute);
    }

so, it seems logical to add some check, as in UrlGenerator::route()

    public function action($action, $parameters = array(), $absolute = true)
    {
        if ($this->rootNamespace && ! (strpos($action, '\\') === 0))
        {
            $action = $this->rootNamespace.'\\'.$action;
        }
        else
        {
            $action = trim($action, '\\');
        }

        if ( !is_null($route = $this->routes->getByAction($action)))
        {
             return $this->toRoute($route, $parameters, $absolute);
        }
        throw new InvalidArgumentException("Action {$action} not defined.");

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions