CodeIgniter Forums
How to get method name in view ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to get method name in view ? (/showthread.php?tid=88396)



How to get method name in view ? - sknair143 - 09-04-2023

Hello,

I want to get the method name in the view to display a CSS class based on that.

I have a method like "employees/documents" (Employees - Controller, Documents - method)

In view how i can get the method name "documents" ?, is there any built-in function from codeigniter ?


Right now i created custom_helper.php :

PHP Code:
<?php

if (!function_exists("get_url")) {

    function get_url(): string
    
{

        $url $_SERVER['REQUEST_URI'];

        $exp explode("/"$url);


        return $exp[3];


    }







and in view 

Code:
<?php echo (get_url() == 'documents') ? 'active' : ''; ?>



This works !, but if any parameters come witht this url, i need to edit again. is there any alternative way which you can suggest ?


RE: How to get method name in view ? - kenjis - 09-04-2023

You can get it from the router.
PHP Code:
\Config\Services::router()->methodName() 



RE: How to get method name in view ? - sknair143 - 09-04-2023

(09-04-2023, 01:09 AM)kenjis Wrote: You can get it from the router.
PHP Code:
\Config\Services::router()->methodName() 

Can i call this directly from view or i have to add in custom_helper code ?


RE: How to get method name in view ? - kenjis - 09-04-2023

I recommend you create a custom helper.
Then, if the behavior of the router changes, you will need to update the helper code only.


RE: How to get method name in view ? - sknair143 - 09-04-2023

(09-04-2023, 01:25 AM)kenjis Wrote: I recommend you create a custom helper.
Then, if the behavior of the router changes, you will need to update the helper code only.

Okay, thanks.


RE: How to get method name in view ? - InsiteFX - 09-04-2023

Here's a quick helper to get you going.

PHP Code:
<?php

/**
 *  getControllerName ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('getControllerName'))
{
    /**
    * getControllerName ()
    * -------------------------------------------------------------------
    *
    * @return string
    */
    function getControllerName() : string
    
{
        $router service('router');
        return $router->controllerName();
    }
}

/**
 *  getMethodName ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('getMethodName'))
{
    /**
    * getMethodName ()
    * -------------------------------------------------------------------
    *
    * @return string
    */
 
function getMethodName() : string
 
{
        $router service('router');
        return $router->methodName();
 }
}

// End of utility_helper Helper.

/**
 * -----------------------------------------------------------------------
 * Filename: utility_helper.php
 * Location: ./app/Helpers/utility_helper.php
 * -----------------------------------------------------------------------
 */