0

I have a route that should generate an href for an anchor tag but I am getting no href:

<a href="" style="color:white !important" class="btn btn-info postlist">Update</a>

My code for above is :

data[i]["confirm"] = '<a href="<?=route_to('updatePost', 1) ?>" style="color:white !important" class="btn btn-info postlist">Update</a>';

My route is :

//$routes->add('post/(:id)', 'App/Controllers/Post::updatepost/$1');
$routes->add('post/(:id)', 'Post::updatepost/$1', ['as' => 'updatePost']);

I am expecting something like this

Noted: tried the unnamed and named way both didnt generate any href

guradio
  • 15,524
  • 4
  • 36
  • 57
  • 1
    Even their example ( with the typo) doesn't work. Seems the moment you add on a (:id) and friends, it throws an error. I'd have to dig into the code but I am a little busy at the moment. If you leave it simple like /post , that works – TimBrownlaw Apr 26 '20 at 14:37
  • (:any) works. Have you tried that, yeah I didn't quite give up yet :) – TimBrownlaw Apr 26 '20 at 15:03
  • Note quite sure on your usage of data[i]["confirm"] = " link code ". is that meant to be $data[$i]['confirm']? – TimBrownlaw Apr 26 '20 at 15:05
  • @TimBrownlaw im here to learn especially with routing i need to improve on that part. as for the index its `i` not `$i`.. i didnt try this yet will try this i need to focus on the other answer :) – guradio Apr 26 '20 at 16:22

1 Answers1

1

The short answer is (:id) isn't supported. It's been deprecated in favour of using (:num)

So the quick fix is to use (:num) instead of (:id)

It is the same.

A temp fix is to change a core file if you really really really need to.

Disclaimer: It is STRONGLY ADVISED, NOT to alter Core Files. Do so at your own Risk

In the file /system/Router/RouteCollection.php - LINE 117

Was:

/**
 * Defined placeholders that can be used
 * within the
 *
 * @var array
 */
protected $placeholders = [
    'any'      => '.*',
    'segment'  => '[^/]+',
    'alphanum' => '[a-zA-Z0-9]+',
    'num'      => '[0-9]+',
    'alpha'    => '[a-zA-Z]+',
    'hash'     => '[^/]+',
];

If you really need it, it Could be:

/**
 * Defined placeholders that can be used
 * within the
 *
 * @var array
 */
protected $placeholders = [
    'any'      => '.*',
    'segment'  => '[^/]+',
    'alphanum' => '[a-zA-Z0-9]+',
    'num'      => '[0-9]+',
    'alpha'    => '[a-zA-Z]+',
    'hash'     => '[^/]+',
    'id'       => '[0-9]+'
];

The change is to add the 'id' entry which mimics the 'num'.

It would be MUCH SAFER to simple Change all references to (:id) to (:num)

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • i see.. its like the :id is a reserve word ? or something like that? I didnt think of it that way. will try this one as soon as i finish the other one :) happy coding mate.. youre a great help i appreciate it – guradio Apr 26 '20 at 16:23
  • 1
    I think it's been deprecated in favor of using (:num) as it is what an (:id) is meant to be and it's been "leftover" in the documentation... I'm adding bug reports as we speak. – TimBrownlaw Apr 26 '20 at 16:25
  • i see noted on this . Im was wondering why it didnt work. there are still some bugs in the version. Do you recommend it for production? – guradio Apr 26 '20 at 16:28
  • 1
    Well I'm working on a project for release. It's been fine so far. Just Change (:id) to (:num) and be done with it... I can see why it was intentionally removed. It's just hanging about in the documentation. I'm kind of liking it and would have cringe attacks going back to CI 3. Plus if there are any bugs - they'll get sorted. You are doing well in finding "issues" :) – TimBrownlaw Apr 26 '20 at 16:30
  • thank you mate great help. I couldn't think of those fix :) – guradio Apr 26 '20 at 16:38
  • 1
    Well we've both learned some new stuff today. If you never asked I would have not found this stuff out :) – TimBrownlaw Apr 26 '20 at 16:55