CodeIgniter Forums
extend url_to helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: extend url_to helper (/showthread.php?tid=90714)



extend url_to helper - sjender - 04-22-2024

Hi,
I would like to extend the url_to() helper function.
This is what I did...
PHP Code:
<?php

use CodeIgniter\Router\Exceptions\RouterException;

function 
url_to(string $controller, ...$args): string
{
    if(empty($_GET['ref'])){
        return original_url_to($controller, ...$args);
    }

    /* EXTRA CODE */
}

//This is a copy of the original CI4 url_to function
function original_url_to(string $controller, ...$args): string
{
    if(!$route route_to($controller, ...$args)){
        $explode explode('::'$controller);

        if(isset($explode[1])){
            throw RouterException::forControllerNotFound($explode[0], $explode[1]);
        }

        throw RouterException::forInvalidRoute($controller);
    }

    return site_url($route);


But I would prefer I didn't have to make a copy of the original function, but refer directly to the CI4 function instead,
How can do this?


RE: extend url_to helper - datamweb - 04-22-2024

See https://codeigniter4.github.io/CodeIgniter4/general/helpers.html#id12

Or use https://codeigniter4.github.io/CodeIgniter4/extending/common.html


RE: extend url_to helper - kenjis - 04-22-2024

(04-22-2024, 07:01 AM)sjender Wrote: But I would prefer I didn't have to make a copy of the original function, but refer directly to the CI4 function instead,
How can do this?

You can't. Because we can have only one global function.
If you don't need to change the behavior of the uri_to(), I recommend you add a new function like my_url_to().