One Controller for multiple Single Pages
Permalink 4 users found helpfulIs it possible to use a single controller for multiple single pages? If possible then whats is the process?
Additionally how can I call a single page into the view method of controller? I'm aware about the controller naming convention. But still get "Page not found".
Citytech
You are probably better off just keeping to one controller per view, which violates basic MVC principals, but it's kind of just the way it is.
Put most of your controller logic into a library, then inherit that library into each of your (now minimal) single page controllers and override/add as you need to.
How to get that:
You move the controller file to a library (/libraries/controllers/single_page_handle) and then in the old controller.php you remove all code and add this
Loader::library('controllers/single_page_handle/controller', 'package_handle');
I have a controller which name is event_type_details.php
Path of this controller is /controllers/dashboard/pkgname/event_type_details.php
class DashboardC5EventEventTypeDetailsController extends Controller { /* code goes here */ }
I have a single page with the same name above & the path is /single_pages/dashboard/pkgname/event_type_details.php
When I try to open this page it shows "Page not found!". Am I doing something wrong?
Citytech
You also need to register the single page with C5 when the package is installed.
Have a look at the package controller and single page & controller paths for a marketplace package. phpinfo comes to mind as I was only working on it a couple of weeks ago and it has one single page.
If the package is called c5_event then the path looks OK.
Ans: The package is called c5event. No underscore between c5 & event.
You also need to register the single page with C5 when the package is installed.
Ans: How could I do this?
Citytech
Try
class DashboardC5eventEventTypeDetailsController extends Controller {
See
https://github.com/concrete5/concrete5/blob/master/web/concrete/libr...
Thanks for your help so far. I've just solved the issue by using $this->render('path/file'); in the same controller. Previously I was creating 2 controllers for add/edit page & listings page. But now I'm just using only one controller & define a function to execute the other. Thanks once again.
Citytech
I'm glad you got this working. Having multiple views ("single_pages") for one controller file is something I do a lot (I'm the person that contributed the patch that @mkly links to above). You might be interested in this boilerplate code I have available, which makes heavy use of this pattern, and also documents a lot of best practices and C5 "gotchas" in the code comments:
https://github.com/jordanlev/c5_boilerplate_crud...
-Jordan
Citytech
To a particular view file as of 5.5 due to this commit
https://github.com/concrete5/concrete5/commit/c5c08e351e14eb5d6d03ba...