-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: shaarli controllers #631
Conversation
We definitely need such a View/Controller system to tidy up the codebase. I'm not too familiar with how MVC-like patterns are implemented in PHP, as I've mostly been using Python libs & frameworks for Web-related musings, and they have a slightly different approach when it comes to how applications are designed (MVC with some twists). Here is a possible component repartition:
This would amount to:
If I get things right, the proposed |
I'm familiar with Symfony in PHP, and Spring and Struts in Java. All of them don't let you worry about what's done in modelsFrameworks usually use an ORM, but we only have views/controllerI started by calling it views, but they do more that just rendering the display. In Symfony, views are templates, and in my understanding of MVC, view is the part where the page is displayed. Here, it handles the user request and acts depending on it. It also call the template engine rendering, though. Otherwise, yes, your 2 bullet points are right. routesNot sure how they're handled in frameworks, but here, the router only has to give which view/controller should be called, based on the request (post/get request todo). HTTP info will be passed anyway.
Yes
Hmm each view/controller will have access to their main class objects, including LinkDB, but also the config manager, the plugin manger, etc.. Then all necessary data will be passed to the template, as it is done in the current
Yes, we need an improvement on this, since all pages (or actions) aren't yet handled by the router.
ConfigManager, and other objects, so they'll basically handle everything done in index.php in their own objects. |
I have something more or less working, and I believe it's looking good. It also made me realize that we abuse of redirections, while we could just call the appropriate templates. Working on this in the future would be easier, and could have good impact on performances. Anyway, it's going to take me forever to unit test it, but I'll go for it. |
Indeed the MVC as used in Symfony (or at least a simplified version of it) seems to be the way to go. If you are looking for inspiration, there are some popular micro-frameworks usually presented as routers/controllers. Their primary role is actually to map a route to a function (as you described above) and then you do what you want in it (rendering a template, reading or writing DB, etc.). The Slim Framework is one of them (that I particularly like). The documentation is quite short to read and could be useful. |
Closing - see #655 |
Our main problem with Shaarli's codebase from the beginning, is that almost everything is contained in
index.php
. We worked a lot to put specific code in their own classes/functions in/application
.Now, I think it's time to go further and split
index.php
code into specific classes designed for any request. This means:renderPage()
which is about 1k line of codeRouter
class which doesn't handle every action, for nowThis PR is an example for the page
login.html
. I'm willing to work on this for the rest of Shaarli, but I'd like your opinion on the implementation and technical choices before going further.Basically:
Controller
abstract class, which is kind of container, handling all of our global classes (configuration, page rendering, plugins, request parameters, etc.).index.php
, a specific Controller is called for any action, based on the Router page definition.redirect()
: if a header redirection is needed.render()
which will render the page through the template engine.