1

I am trying to implement a helper function which helps me sort of 'add' routes (which map to controllers/actions) in an MVC application.

My index.php file looks like this.

//  use block
use App\Router;
use App\Config;
use App\Core\Database;
use App\Bootstrap;

// dependencies
$router    = new Router;
$config    = new Config;
$database  = new Database($config);
$bootstrap = new Bootstrap($router, $database);

// routes
require '../App/Routes.php';


$bootstrap->router->dispatch($_SERVER['QUERY_STRING']);

And my 'routes' file has one route in it, my routes look like this.

$bootstrap->router->add('/home', ['controller' => 'home', 'action' => 'index']);

I would prefer to simply add roots into my file like this...

route('/home', ['controller' => 'home', 'action' => 'index']);

So I need to make a helper function

question

Thanks to psr-4 autoloading I know how to pull in classes whenever I need them, however this is less clear when it comes to plane old functions. How can I cleanly add my helper functions into my MVC Framework without adding require files everywhere?

Many thanks in advance for your time and consideration!

Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80

2 Answers2

5

To have helper functions available globally, you have to include them globally. One way to do this is in your application entry point (usually index.php), you require the file that has your helper functions.

Laravel, for example, does this and has a wide variety of helpers: https://laravel.com/docs/5.1/helpers

Whenever I wrote my own framework for learning purposes, I found that I liked to have my helpers separated into areas of functionality, so what I tended to do is have a folder for helpers, something like /app/helpers and use a bit of code like this in my entry point:

foreach (glob("./app/helpers/*.php") as $helper) {
    require_once($helper);
}

Be aware that this pollutes the global namespace, so be very careful and thoughtful about your naming conventions.

An alternative is to create a Helper class and have methods available statically in them so you could call things like Helper::route() for example and it keeps the functions out of the global namespace.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 1
    Oh man, this is such a good answer, so clear and helpful, it was learning a bit of laravel that sparked this question in the first place, great to know! – Jethro Hazelhurst Jul 28 '16 at 16:49
2

As covered here Autoloader for functions, you have a couple of options, as there are no autoloaders for function.

If you are using composer to manage your dependencies, you can put all of your functions in one file and add a "files" directive to the "autoload" section of your composer.json file. This will cause composer to do the dirty work for you in it's generated autoloader.

Other than that, you can just put the require_once for the file with your functions in it into the index.php for your framework and away you go.

You could even take all of your functions, combine them into a class as static methods and simply call them using Libraryname::functionname(), then when you use this, the autoloader will take care of everything for you just like with any other class.

To recap, no autoloader, but lots of good, solid, clean options for making this work.

Community
  • 1
  • 1
Reid Johnson
  • 1,394
  • 14
  • 20