Dynamic controllers?

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

Dynamic controllers?

Post by kinerity » Wed Sep 12, 2018 8:07 am

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 Truscott - Community Team

User avatar
martti
Registered User
Posts: 618
Joined: Thu Jul 31, 2014 8:23 am
Location: Belgium

Re: Dynamic controllers?

Post by martti » Wed Sep 12, 2018 10:14 am

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
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 11:35 am

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?¿?
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1841
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Dynamic controllers?

Post by kinerity » Wed Sep 12, 2018 1:18 pm

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 Truscott - Community Team

User avatar
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 24597
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: Dynamic controllers?

Post by Paul » Wed Sep 12, 2018 1:47 pm

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.
Knock knock
Race condition
Who's there?

My BlogMy Photosmy phpBB Extensionscustom phpBB work & Development

User avatar
3Di
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 2:15 pm

kinerity wrote:
Wed Sep 12, 2018 1:18 pm
My routes are dynamic
Let's see those routes then.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
GanstaZ
Registered User
Posts: 415
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Dynamic controllers?

Post by GanstaZ » 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).
"When answer lies in the question,.. question becomes redundant!"

User avatar
3Di
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 2:43 pm

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.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1841
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Dynamic controllers?

Post by kinerity » Wed Sep 12, 2018 2:52 pm

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 Truscott - Community Team

User avatar
3Di
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 2:57 pm

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.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1841
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Dynamic controllers?

Post by kinerity » Wed Sep 12, 2018 3:05 pm

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 Truscott - Community Team

User avatar
3Di
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 3:14 pm

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.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 1841
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Dynamic controllers?

Post by kinerity » Wed Sep 12, 2018 3:28 pm

That gives me a good starting point. I'll see what I can work out. Thanks!
Kailey Truscott - Community Team

User avatar
3Di
Registered User
Posts: 12899
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Dynamic controllers?

Post by 3Di » Wed Sep 12, 2018 3:34 pm

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

And yes, major credits to posey. +1
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
GanstaZ
Registered User
Posts: 415
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Dynamic controllers?

Post by GanstaZ » Wed Sep 12, 2018 3:56 pm

There should be 1 similar topic here on this forum about dynamic routes.. Yeah.. here it is Dynamic routing.
"When answer lies in the question,.. question becomes redundant!"

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 14 guests