I have registration.phtml
file in src/Mess/View/pages
and registrationView.php
in src/Mess/View/Views
. How can i specify the relative path to the registration.phtml
file in the registrationView.php
file?
This is code i registrationView.php`
class RegistrationView extends View
{
public ValidationAttributes $validation;
public function __construct(Action $registration)
{
parent::__construct(src/Mess/View/pages/registration.phtml', [
'registration' => $registration,
]);
$this->validation = $registration->validationAttributes([
'login',
'password',
'passwordRepeat',
'firstName',
'lastName',
'email',
'phoneNumber',
'birthDate',
'gender',
'avatar',
]);
}
2
Answers
I assume that you use the given string in the parent::__construct as a path.
You should be getting a parse error because of a missing quote, plus the
src
directory is by no means a child ofViews
:My advice is that you don’t use relative paths, they’re very hard to manage when you start nesting includes. You can use the
__DIR__
the magic constant and thedirname()
function to build absolute paths:Running this from
src/Mess/View/Views/registrationView.php
will print/path/to/project/src/Mess/View/pages/registration.phtml
, which is valid no matter current working directory.But everything will be easier if you create some utility constants that allow you to forget about relatives paths once and for all, as in:
Or just: