Dynamic function names?

I do not know if I formulate this in the right way, but is there such a thing as dynamic function names? In my Controller class I have functions like:

about_usAction, antiqueAction etc …

this gives the following (friendly) url

www.website.com/about_us

This is a multilingual website and I am looking for a way to have the first part of the function name (about_us, antique) dynamic. For example in German about_us is written as uber_uns Now I wonder if there is a way that the (friendly) urls / about_us and / uber_uns can use the same function

How is your routing set up currently? Are you using a package (in an earlier thread you mentioned AltoRouter) or is it custom?

@rpkamp. This is indeed the project where I use AltoRouter

So what’s the problem? You can just define all URLs and pass them the same controller. I don’t see where it has to be dynamic.

@rpkamp. I think I don’t understand you :frowning:

www.mysite.com/about_us and www.mysite.com/uber_uns both should use the same function instead of having two separate functions:

public function about_usAction

and

public function uber_unsAction

I hope I made myself a bit clearer this way?

Can you show what your router setup looks like?

@rpkamp. This is router Class

class Router {
    private $router;

    public function __construct()
    {
        $config = Config::get('routes');
        $this->router = new AltoRouter();

        foreach ($config['routes'] as $route => $options) {
            $this->router->map( 'GET|POST', $route, $options );
        }
    }

    public function dispatch($route = '/')
    {
        $match = $this->router->match($route);
        if ( $match ) {
            $options = array_merge( $match['target'], $match['params'] );
            $callable = [new $options['controller']($route), $options['action'] . 'Action'];
            $callable($match['params']);
        } else {
            header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
            echo '<pre>', var_dump($this->router, $match), '</pre>';
        }
    }
}

Okay in that case it looks like you could just do the following in your config array:

'about_us' => [
    'controller' => 'about',
    'action' => 'about_us'
],
'uber_uns' => [
    'controller' => 'about',
    'action' => 'about_us'
]

No?

@rpkamp. Ha ok. That were the routes you wanted to see:

return [
    'routes'  => [
        '/admin/login' => [
            'controller' => 'Controller_Auth',
            'action' => 'login',
        ],
		
        '/admin/logout' => [
            'controller' => 'Controller_Auth',
            'action' => 'logout',
        ],	
		
        '/admin/[:action]?/[a:abbr]?' => [
            'controller' => 'Controller_Admin_Page',
            'action' => 'index',
        ],
		
				

        '/[a:action]/[i:id]?' => [
            'controller' => 'Controller_Page',
            'action' => 'index',
        ], 

        '/' => [
            'controller' => 'Controller_Page',
            'action' => 'index',
        ]
		   		
    ]
];

I’m gonna give your example a try and let you know!

@rpkamp. Works great :grin: thanks a lot :+1:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.