PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 30, 2022

[FIXED] How to access the last part of a GET url in CakePHP 3

 August 30, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I'm building my link like this:

$this->Html->link(
    $this->Icon->fa('plus'),
    ['controller' => 'Questions', 'action' => 'add', $questionnaire->id],
    [
        'title' => 'Add'
    ]
);

This creates the desired icon on my browser, with this link:

<host>/questionnaire/questions/add/2

On the "other side", by using for example

$this->request->getParam('action')

I can correctly get "add" as a value.

But how can I access the 2, so the last part of the url?

I've tried with

$this->request->getQueryParams()

and it returns an empty array.


Solution

That depends on your connected routes. If there is a corresponding route that has a route element defined for that value, eg

/questionnaire/questions/add/:id

then you could get the value via that element name, getParam('id').

If the corresponding route does not have a dedicated named element for that value, but instead a *, which is the default for fallback routes connected via fallbacks(), then it will land in the pass parameter, getParam('pass'), which is an array of trailing values, and your value would sit at index 0.

See also

  • Cookbook > Request & Response Objects > Request Parameters
  • Cookbook > Routing > Route Elements
  • Cookbook > Routing > Passed Arguments


Answered By - ndm
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing