2

In my project I have a folder for modules where they can have composer packages The namespace for the module I'm making is Modules\View\View for Modules/View/View.php file

this is the directory layout

Modules
-View
--View.php
--vendor
---autoload.php

when I do this in View.php

public function __construct(){
    require_once __DIR__.'/vendor/autoload.php';
    $this->loader = new Twig_Loader_Filesystem('design');
    $this->twig = new Twig_Environment($this->loader);
}

I get an error: Fatal error: Class 'Modules\View\Twig_Loader_Filesystem' not found in Modules/View/View.php on line 12

So I assumed this is because the View.php file has a namespace and I went to /Modules/View/vendor/composer/autoload_namespaces.php and changed

'Twig_' => array($vendorDir . '/twig/twig/lib')

to

'Modules\View\Twig_' => array($vendorDir . '/twig/twig/lib')

and it still doesn't work, however I'm still 100% sure it's because of the namespace because if you try to load it in a file without a namespace it works but as soon as you add it it gives that error

1 Answer 1

1

What is the namespace of Twig_Loader_Filesystem?

If it does not have a namespace, you can load it using the root namespace (\):

$this->loader = new \Twig_Loader_Filesystem('design');

You can also use the use keyword:

namespace Modules\View;

use Twig_Loader_Filesystem;
use Twig_Environment;

class View {

  public function __construct(){
    require_once __DIR__.'/vendor/autoload.php';
    $this->loader = new Twig_Loader_Filesystem('design');
    $this->twig = new Twig_Environment($this->loader);
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.