Welcome Guest, Not a member yet? Register   Sign In
Controller Constructor Parameter init
#1

Hi,
I would like to know if it is possible to init the controller with route parameter issued to each method within the controller? Sorry if this is stated in the documentation, I have read through it quite throughly and haven't come across a specific answer to this but could i possibly instantiate a controller class with a routing parameter set class wide, such as...
Code:
__constructor(private int $event_id) {}

rather than having to set... in each method...
Code:
$this->event_id = $event_id;

Is this possible or not?
Thanks in advance
Reply
#2

The framework does not support named route parameters and autowiring. So it's not possible in the current implementation.
Reply
#3

Probably it would be possible if you do the following two things:

1. Extend Router to implement named route parameters
2. Extend CodeIgniter class to inject the named parameters when creating a controller
Reply
#4

Looks like they may be moving towards implementing this feature from the looks of the more recent Uri Routing documentation
https://codeigniter.com/user_guide/incom...ble-syntax
PHP Code:
use App\Controllers\Product;

$routes->get('product/(:num)/(:num)', [Product::class, 'index']);

// The above code is the same as the following:
$routes->get('product/(:num)/(:num)''Product::index/$1/$2'); 

Thanks any way for your prompt replies. If i do find time to help out with this feature i will surely help out any way i can. Don't think it is an issue which causes mny pains but think it might be nice for future iterations, helping to clean up controller classes.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB