3

I am a PHP developer and I used to build web applications with one PHP file per page for a very long time. This resulted in duplicated logic and messy code (even though I didn't know it at the time).

Then one day a co-worker introduced me to the MVC methodology of organising your web application code base. I also learnt about the front controller pattern during this time and I was amazed at how it simplified everything. Whenever I build web applications now I route all web requests to a single PHP file which uses the front controller pattern to create request and response objects, then dispatch the request to the appropriate controller and action. I am very satisfied with the way everything works and I feel as though my code is cleaner for it!

I have recently started writing a couple of command line applications for fun in PHP. Within these I have tried to use the front controller pattern but it doesn't feel quite right. The concept of routing all requests to one file is a little bit redundant because there can only be one entry point (by calling php -f myapp.php -- argumentone argumenttwo).

It also feels to me as though the concept of routing using controllers and actions is a little bit odd as well. When I have stuck to the MVC pattern with command line applications I almost always end up with one controller with only one or two actions. This seems like a signal that I am not using the front controller (or possibly even the MVC pattern) correctly.

Is there a better way to handle routing and dispatching requests within a command line application? Does the front controller and MVC pattern only apply to web applications? Is there a better design pattern I should be using for command line applications?

Thank you in advance and apologies for my long winded ramble! :D

TLDR - Is there a pattern similar to the front controller pattern for developing command line applications? At the moment the front controller pattern seems like an odd fit.

  • 1
    I doubt if it qualifies as a pattern but the Symfony console component is quite popular and well documented: symfony.com/doc/current/components/console/index.html. And while you may have to squint a bit to see it, the front controller pattern is in there along with individual command files representing controller actions. – Cerad May 19 '16 at 13:36
  • Thank you so much, I've been reading the Symfony console documentation and this is definitely a step in the right direction! I think I need to learn more about the format of command line applications (such as the usages of and differences between commands, arguments and options). – user229034 May 19 '16 at 14:58
1

I'm going to answer my own question after a day of research. In the end this ended up looking more towards cron jobs which support MVC web applications (which isn't exactly the same as my original question but yielded some interesting information none-the-less). Anyway, here's what I found:


Q. The concept of routing all requests to one file is a little bit redundant because there can only be one entry point?

A. Correct, it is redundant because command line applications can only have one entry point, the executable file itself. You do not need to use the Front Controller pattern in your executable file unless you have sub commands. A lot of simple command line applications are essentially modelled in the same way as a large Bash script. You may want to use the Front Controller pattern if your command line application has sub commands.

Q. It also feels to me as though the concept of routing using controllers and actions is a little bit odd as well. When I have stuck to the MVC pattern with command line applications I almost always end up with one controller with only one or two actions. This seems like a signal that I am not using the front controller (or possibly even the MVC pattern) correctly?

A. That is also correct. If you are building command line applications to support MVC web applications it does not make sense to put your command line application logic inside a controller. In terms of a web application, controllers are for web requests, not command line applications. It should instead go in its own class. Sometimes these classes are referred to as "Tasks" or "Commands" or "Console Commands" within web framework communities.

Try leaving your controllers just for web requests and adding those sort of things to a Commands namespace or similar. There is one caveat here though, it is perfectly fine to use the M from MVC (Models) in your commands however. From my research it appears uncommon to utilise the V (views) in console commands either, just handle the output with simple echos and such.

Within your Command class it appears that it is very common to have only one method called run or execute. This contains the logic of your command and this leads onto your next question.

Q. Is there a better way to handle routing and dispatching requests within a command line application?

A. Yes, leave the MVC pattern alone for a web frameworks as it works well. For running cron jobs add another C to your acronym, that C should represent the word Console. In this new MVCC (that it used in jest, I'm sure that is already a proper acronym for another pattern) web requests come in through a front controller and so on as usual. For cron jobs create a second entry point (outside of the webroot directory) which triggers a specific Console class's run method to be executed.


I hope this will help someone, I've included some suggested reading material below:

http://maxoffsky.com/code-blog/practical-laravel-using-cron-jobs-in-laravel/ http://symfony.com/doc/current/components/console/index.html http://book.cakephp.org/2.0/en/console-and-shells.html https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html

  • 2
    This answer is only correct for a certain class of console applications. Specifically, those where you launch them with a set of args, they run, and there is no user interaction. Many console applications have quite complex user interactions and could certainly benefit from an MVC architecture. MVC isn't just for web. It's a useful pattern anytime you want to separate logic from presentation. In this case, if you use a MVC pattern, you could easily write a GUI for your program later. – RubberDuck May 21 '16 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy