It’s the ‘custom’ helper!
Not really discoverable. Why not make use of namespaced functions or classes instead of magically defining functions like this?
Imagine it’s 5 years from now and you’re asked to make changes to this code. Would you still know what goes where?
That’s a good idea but can you do an example of how i could integrate that in my controller! Here is a simple one:
<?php if (!defined('BASE_PATH')) exit('No direct script access allowed');
class Page extends Controller
{
public $page;
public function __construct()
{
$this->load_helper(['view']);
$this->load_helper(['url']);
$this->load_helper(['session']);
$this->load_helper(['date']);
$this->setting = $this->model('Settings');
$this->page = $this->model('Pages');
}
public function index()
{
redirect('error');
die();
}
public function p($id)
{
$getSettings = $this->setting->getAll();
$getCategories = $this->setting->getCategories();
$getPageByName = $this->page->getPageByName($id);
$data = [
'getSettings' => $getSettings,
'getCategories' => $getCategories,
'getPage' => $getPageByName
];
if($getPageByName == false) { redirect('error'); die(); }
$this->view('page', $data);
}
}
I would use Dependency Inversion (the D in SOLID) to inject any collaborators I need into the class, instead of letting the class initialise them. That way you gain testability of the class as well, as you can inject mocks instead of the real thing, so you don’t have to interact with a database for example in your tests, which make them a lot faster.
As for your controller, I would do something like this:
<?php
// Not sure about the next line. It shouldn't exist at all, because all your
// PHP code should live _outside_ the publicly accessible directories
if (!defined('BASE_PATH')) exit('No direct script access allowed');
<?php
class PageController
{
private $template;
private $urlGenerator;
private $settings;
private $pageRepository;
public function __construct(
TemplateEngine $template,
UrlGenerator $urlGenerator,
SettingsRepository $settings,
PageRepository $pageRepository
) {
$this->template = $template;
$this->urlGenerator = $urlGenerator;
$this->settings = $settings;
$this->pageRepository = $pageRepository;
}
public function index()
{
return new Response(
$this->template->render('error'),
500
);
}
public function page($pageName)
{
try {
$page = $this->pageRepository->getPageByName($pageName);
} catch (NoSuchPageException $e) {
return new Response(
sprintf('Page "%s" not found', $pageName),
404
);
}
return new Response(
$this->template->render('page', [
'page' => $page,
'categories' => $this->settings->getAllCategories(),
// no passing of _all_ settings here, only pass the settings
// your template really needs.
]),
200
);
}
}
And then wire the controller up with a DI container, like Symfony’s DI container, Aura DI container, whichever you like best.
I recently saw a talk on YouTube that also talks about this subject. You might find that interesting as well Marco Pivetta - From helpers to Middlewhere. He has a lot of other good talks as well, like Extremely defensive PHP.
Yes they do live outside and are not publicly accessible but i just kept it their in case something was to ever break so its just a second measure.
Ok thanks that looks very good and much more up to date method than mine but what would a TemplateEngine file look like such as would i just put it in the helper folder/where do i put it/what does it look like?
Can you do a simple example.
This is what my current helper view looks like:
/**
* @param $paths
* @param $data
* @return mixed
*
* Extends the View by loading another file relative the base Views Path.
*/
function extend_view($paths, $data)
{
// Set each index of data to its named variable.
foreach($data as $key => $value) {
$key = $value;
}
foreach ($paths as $path) {
include (VIEWS_PATH . $path . '.php');
}
}
I would’t write a template engine myself, I’d use Twig.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.