Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

what's the problem?

we I visit the local site Stores,there comes out a error:

Fatal error: Class 'Stores\Frontend\Controllers\Phalcon\Tag' not found in E:\wamp\www\stores\apps\frontend\controllers\IndexController.php on line 10
Call Stack
#   Time    Memory  Function    Location
1   0.0010  146304  {main}( )   ..\index.php:0
2   0.0020  157552  handle ( )  ..\index.php:29
3   0.0040  197560  Stores\Frontend\Controllers\IndexController->initialize( )   ..\index.php:0

IndexController.php

<?php

namespace Stores\Frontend\Controllers;

class IndexController extends ControllerBase
{
 public function initialize()
    {
        $this->view->setTemplateAfter('main');
        Phalcon\Tag::setTitle('Welcome');
        parent::initialize();
    }
    public function indexAction()
    {

    }

}

While invo project run the same code is ok.



16.8k
Accepted
answer
edited Aug '14

Try to add \ at the beginnig of this line:

Phalcon\Tag::setTitle('Welcome');

So you should get this:

\Phalcon\Tag::setTitle('Welcome');

Did it helped?



6.4k
edited Aug '14

Yes, it works. Why? There is not in project invo and it works.



6.4k

Thank you very much.



16.8k

Which file of invo and which line? Could you provide a link?



6.4k
edited Aug '14

https://github.com/phalcon/invo/blob/master/app/controllers/IndexController.php

<?php

class IndexController extends ControllerBase
{
    public function initialize()
    {
        $this->view->setTemplateAfter('main');
        Phalcon\Tag::setTitle('Welcome');
        parent::initialize();
    }

    public function indexAction()
    {
        if (!$this->request->isPost()) {
            $this->flash->notice('This is a sample application of the Phalcon PHP Framework.
                Please don\'t provide us any personal information. Thanks');
        }
    }
}


16.8k

Yes,it works.Why?

It is about namespaces. The highest level of all namespaces is \. It is kind of root. Your file is not in the root.

Imagine that your file begins with:

namespace \Stores\Frontend\Controllers; /look, there is a slash at the beginning

So your file is in \Stores\Frontend\Controllers namespace.

So when you are creating a class and you don't type \ at the beginnig (e.g. Phalcon\Tag as you did) PHP will be looking for a class relative to current namespace. So it will be looking for \Stores\Frontend\Controllers + Phalcon\Tag = \Stores\Frontend\Controllers\Phalcon\Tag and such a class does not exist.

However, if you specify this \, so when you write \Phalcon\Tag then PHP will be simply looking for \Phalcon\Tag which is the class you are looking for.

The thing is that as a rule we do NOT specify first slash in namespace clause at the beginnig of the file

namespace Stores\Frontend\Controllers; //so you don't write a slash but imagine that it is there

Suggesting to read all articles from http://php.net//manual/pl/language.namespaces.php



16.8k

The invo code works because there is no namespace specified at the top of the file. So the current namespace of the file is the root = \.

So:

\ + Phalcon\Tag = \Phalcon\Tag



6.4k

Thank you!