Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.
This repository was archived by the owner on May 22, 2024. It is now read-only.

Can't get parameters' default value to work #33

Description

@zessx

I'm trying to bind those url to the same route :

  • posts
  • posts/2014

I used default values for my :year parameter, as defined in documentation :

'app' => array(
    'routes' => array(
        'posts/:year' => array(
            '@name' => 'posts',
            'action' => 'posts/index',
            'year' => 'all',
        ),
    ),
),

Here are the results :

Build url Result
Atomik::url('@posts', array('year'=>2014)) domain.tld/posts/2014
Atomik::url('@posts') domain.tld/posts/:year

How could we remove this unused/unusable /:year from the url ?
We should :

  • use Atomik::url('@posts') without parameter
  • get domain.tld/posts as the rewritten url
  • get year parameter set to all in our action file

I made an attempt to get it work (see this fork) :

// injects parameters into the url
if (preg_match_all('/(:([a-z0-9_]+))/i', $action, $matches)) {
    for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
        if (array_key_exists($matches[2][$i], $params)) {
            $action = str_replace($matches[1][$i], $params[$matches[2][$i]], $action);
            unset($params[$matches[2][$i]]);
        } elseif (array_key_exists($matches[2][$i], $default)) {
            // default value is found, remove he related part from url
            $action = preg_replace('/:'.$matches[2][$i].'\/?/i', '', $action);
        }
    }
}

Currently, it remove /:year from the url, but the action file doesn't receive any year default value.
As possible side-effects, we must think about people using Atomik without url-rewriting.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions