donboe
October 12, 2018, 10:22am
1
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
rpkamp
October 12, 2018, 10:59am
2
How is your routing set up currently? Are you using a package (in an earlier thread you mentioned AltoRouter) or is it custom?
donboe
October 12, 2018, 7:04pm
4
@rpkamp . This is indeed the project where I use AltoRouter
rpkamp
October 12, 2018, 8:42pm
5
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.
donboe
October 13, 2018, 7:04am
6
@rpkamp . I think I don’t understand you
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?
rpkamp
October 13, 2018, 7:04am
7
Can you show what your router setup looks like?
donboe
October 13, 2018, 7:08am
8
@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>';
}
}
}
rpkamp
October 13, 2018, 7:28am
9
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?
donboe
October 13, 2018, 7:48am
10
@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!
donboe
October 13, 2018, 8:03am
11
@rpkamp . Works great thanks a lot
1 Like
system
Closed
January 12, 2019, 3:04pm
12
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.