Dynamic controllers?

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Dynamic controllers?

Post by Kailey »

I have a function that creates a dynamic route (finally got those working, thanks to the Pages extension! :D ) and specifies a default controller.

Code: Select all

$route->setDefault('_controller', 'kinerity.extension.controller1:display');
What I need to do is load a different controller (controller2) based on if a parameter is supplied in the link. For example, https://www.local.host/route?p=1. Creating the link is not the problem. All controllers have the same display() function as well.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
martti
Registered User
Posts: 911
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Dynamic controllers?

Post by martti »

Maybe just keep it to one controller and just check the parameter inside your controller? (Inject the request class in the constructor)

Code: Select all

$p = $this->request->variable('p', 0);

if ($p)
{
    // do something here when parameter was not zero
}
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

Yes, a controller is dynamic when there is a dynamic route.
kinerity wrote: Wed Sep 12, 2018 8:07 am All controllers have the same display() function as well.
What you mean here?
Each controller (in case) have to have the same code?¿?
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Dynamic controllers?

Post by Kailey »

martti wrote: Wed Sep 12, 2018 10:14 am Maybe just keep it to one controller and just check the parameter inside your controller?
I had thought about doing this, but my controller files are already 1000+ lines each. I was trying to keep them as small as possible. I can, just wasn't sure if there was another way.
3Di wrote: Wed Sep 12, 2018 11:35 am Yes, a controller is dynamic when there is a dynamic route.
Then can you give me an example? My routes are dynamic but I can't figure out how to tell the route code to load controller2 if my parameter is present in the URL.
3Di wrote: Wed Sep 12, 2018 11:35 am
kinerity wrote: Wed Sep 12, 2018 8:07 am All controllers have the same display() function as well.
What you mean here?
Each controller (in case) have to have the same code?¿?
No, different code. What I was referring to was the name of the function in each controller file is display().

Code: Select all

public function display($route)
{
    // Do something
}
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28616
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Dynamic controllers?

Post by Paul »

This sounds more that if they aren't trully dynamic, but still kinda static. In that case I would like to suggest to create seperate controllers that can handle one thing, instead of using dynamic controllers that can handle a lot.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

kinerity wrote: Wed Sep 12, 2018 1:18 pm My routes are dynamic
Let's see those routes then.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: Dynamic controllers?

Post by GanstaZ »

Why not try what martti mentioned above? By injecting request class you can load whatever method you want (if the param is not null, load method 2 or 3 or whatever).
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

GanstaZ wrote: Wed Sep 12, 2018 2:35 pm Why not try what martti mentioned above? By injecting request class you can load whatever method you want (if the param is not null, load method 2 or 3 or whatever).
He/she stated has thought about doing this, but the controller files are already 1000+ lines each.
Tying to keep them as small as possible.


There are many ways to do that, but what is not clear IMO would be the purpose for which we are trying to use a dynamism instead of traditional methods, it is not clear the code ... there 's little clear.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Dynamic controllers?

Post by Kailey »

3Di wrote: Wed Sep 12, 2018 2:15 pm Let's see those routes then.
If you're asking to see the entire current code, I unfortunately can't do that right now (development code at my company). If you want to see how I have routing setup, it's the same as the code for Pages.
GanstaZ wrote: Wed Sep 12, 2018 2:35 pm Why not try what martti mentioned above? By injecting request class you can load whatever method you want (if the param is not null, load method 2 or 3 or whatever).
As I stated above, I was trying to avoid this if possible. I don't mind combining them into 1 controller if it comes to that (which it looks like it might).
3Di wrote: Wed Sep 12, 2018 2:43 pm What is not clear IMO would be the purpose for which we are trying to use a dynamism instead of traditional methods
It saves me from writing 4 controllers per route (currently 4 routes).
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

kinerity wrote: Wed Sep 12, 2018 2:52 pm If you're asking to see the entire current code
No, no time to dig into other('s) extensions of any sort.

Just the routing.yml file should do.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Dynamic controllers?

Post by Kailey »

Code: Select all

kinerity_extension_dynamic_controller:
    resource: kinerity_extension_new_controller # can be any name, it is not used, but must be present
    type: kinerity_extension_route
Like I said, same as pages. :D I don't think the problem is routing, because I can get to controller1.
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

routing.yml

Code: Select all

phpbbstudio_bananas:
    path: /bananas
    defaults:
        _controller: phpbbstudio.bananas.main.controller:handle

phpbbstudio_bananas_prefix:
    resource: routing_bananas.yml
    prefix: /bananas
routing_bananas.yml

Code: Select all

phpbbstudio_bananas_dynamic_courses:
    resource: phpbbstudio_bananas_dynamic_courses
    type: phpbbstudio_bananas_courses

phpbbstudio_bananas_filter:
    path: /filter/{mode}
    defaults:
        _controller: phpbbstudio.bananas.main.controller:filter
services.yml

Code: Select all

imports:
    - { resource: tables.yml }
    - { resource: services_controllers.yml }
    - { resource: services_entities.yml }
    - { resource: services_functions.yml }
    - { resource: services_listeners.yml }
    - { resource: services_operators.yml }

services:
    phpbbstudio.bananas.routing.courses:
        class: phpbbstudio\bananas\routing\course_loader
        arguments:
            - '@phpbbstudio.bananas.operators.course'
        tags:
            - { name: routing.loader }
etc.. ?

I can't show you more since it is a project not yet finished but working, hence lies into a private repo of us.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3726
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay
Contact:

Re: Dynamic controllers?

Post by Kailey »

That gives me a good starting point. I'll see what I can work out. Thanks!
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules

If you have any questions about the rules/customs of this website, feel free to send me a PM.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di »

The Loader it is important.. ;) I can't show you more as I said.

And yes, major credits to posey. +1
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: Dynamic controllers?

Post by GanstaZ »

There should be 1 similar topic here on this forum about dynamic routes.. Yeah.. here it is Dynamic routing.
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
Post Reply

Return to “Extension Writers Discussion”