We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



26.3k
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?



10.3k
edited Aug '14

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



10.3k

Thank you very much.



26.3k

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



10.3k
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');
        }
    }
}


26.3k

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 https://php.net//manual/pl/language.namespaces.php



26.3k

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



10.3k

Thank you!