thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

Lazy loading controller with wildcard #247

Closed jokerham closed 5 years ago

jokerham commented 5 years ago

I have raised a question in stackoverflow about how to set Lazy Loading Controller with wildcard.

https://stackoverflow.com/questions/56763069/how-to-use-wildcard-route-in-league-route-in-lazy-loading-way

I did not get the response. So I checked the source code, but it seems that there is no replacement logic for wildcard in Lazy Loading Controller.

I have changed the source temporarily to resolve this as following by changing Route.php file.

    protected function resolveClass(?ContainerInterface $container = null, string $class)
    {
        if ($container instanceof ContainerInterface && $container->has($class)) {
            return $container->get($class);
        }

        foreach ($this->vars as $key => $var) {
            $class = preg_replace("/\{$key\}/", "$var", $class);
        }

        return new $class();
    }
philipobenito commented 5 years ago

I'm not completely sure what your question is, all routes are already lazy loaded if you define the controller as a string.

On Wed, 26 Jun 2019, 18:52 Dongkyun Ham, notifications@github.com wrote:

I have raised a question in stackoverflow about how to set Lazy Loading Controller with wildcard.

https://stackoverflow.com/questions/56763069/how-to-use-wildcard-route-in-league-route-in-lazy-loading-way

I did not get the response. So I checked the source code, but it seems that there is no replacement logic for wildcard in Lazy Loading Controller.

I have changed the source temporarily to resolve this as following.

` */ protected function resolveClass(?ContainerInterface $container = null, string $class) { if ($container instanceof ContainerInterface && $container->has($class)) { return $container->get($class); }

foreach ($this->vars as $key => $var) {
    $class = preg_replace("/\{$key\}/", "$var", $class);
}

return new $class();

}

`

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/thephpleague/route/issues/247?email_source=notifications&email_token=AAP3E2ME64EG7OF54EQ3I53P4OUFPA5CNFSM4H3UUX62YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G334KPA, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP3E2NDIN5YEXXKQ4Q6AADP4OUFPANCNFSM4H3UUX6Q .

jokerham commented 5 years ago

I wanted the controller name to be a string with the wildcard variable as part of the name.

router->map('GET', '{module}', "Nanitelink\Module\{module}\{module}Controller::index"); As above code, if the request is matched with wildcard "module" I wanted to find the controller by replacing the "{module}" in the controller name.

philipobenito commented 5 years ago

No that's not currently possible. It's unlikely that it will be added as a feature either as the idea with this package was always to explicitly define routes.

On Wed, 26 Jun 2019, 19:27 Dongkyun Ham, notifications@github.com wrote:

I wanted the controller name to be a string with the wildcard variable as part of the name.

'''router->map('GET', '{module}', "Nanitelink\Module{module}{module}Controller::index");'''

As above code, if the request is matched with wildcard "module" I wanted to find the controller by replacing the "{module}" in the controller name.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/thephpleague/route/issues/247?email_source=notifications&email_token=AAP3E2LVZL4YNKF2M47GJ3DP4OYJNA5CNFSM4H3UUX62YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYUNIUA#issuecomment-505992272, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP3E2N6PJX3O35RLJUYSYLP4OYJNANCNFSM4H3UUX6Q .