Welcome Guest, Not a member yet? Register   Sign In
Is there a way to get the current method arguments?
#1

I am using this static pages controller from the documentation with this route definition:

PHP Code:
// static pages
$routes->get('/{locale}/(:any)''PagesController::show/$1', ['priority' => 1]); 

I am adding a css class on the links to existing pages in my navigation. For most of the links I can easily test the name of the controller, but for the static pages, I need to know which page is called. Is there a way to get that value? (I experimented with getMatchedRoute(); and it looks like I can get from one of the array value the full route, eg
Code:
\App\Controllers\PagesController::show/about

but I was wondering if there was a way to get the argument directly?
Reply
#2

Try URI object? site.com/profile/103 See segments array

PHP Code:
$this->request->getUri()->getSegments() 
Code:
array:2 [▼
  0 => "profile"
  1 => "105"
]

Code:
CodeIgniter\HTTP\SiteURI {#25 ▼
  #uriString: null
  -baseURL: null
  #segments: array:2 [▼
    0 => "profile"
    1 => "105"
  ]
  #scheme: "http"
  #user: null
  #password: null
  #host: "ci-demo-htmx"
  #port: null
  #path: "/profile/105"
Reply
#3

(This post was last modified: 01-06-2024, 08:09 AM by kcs.)

Thanks @ozornick. In the end, here's what I did inside a helper:

PHP Code:
function isCurrentMenu(){
    $controller_name = \CodeIgniter\Config\Services::router()->controllerName();

    switch ($controller_name) {
        case '\App\Controllers\Home':
            return 'home';
            break;
        case '\App\Controllers\NewsController':
            return 'news';
            break;
        case '\App\Controllers\PagesController':
            // the name of the page is the last part of the url
            $urluri_string();
            $parts explode('/'$url);
            return end($parts);
            break;
    }



 
And in my view, on the links where I need the CSS class, I call it like that:

Code:
<a href="" class="<?= isCurrentMenu() == 'about' ? 'current' : '' ; ?>">about</a>

Not sure it is the best way, but it does what I need Smile
Reply
#4

See function if ( url_is('about*') ) ...
Reply
#5

Thanks for the suggestion @ozornick Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB