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

How to implement phalcon classes?

I sees many pages in the Phalcon's document said implement but none of them show how to implement.

For example: URL class.

I was extended my URL generator class in app/Extend/Phalcon/Mvc/Url.php

<?php

namespace Extend\Phalcon\Mvc;

class Url extends \Phalcon\Mvc\Url
{

    /**
     * Returns the prefix for all the generated urls. By default /
     *
     * @return string
     */
    public function getBaseUri()
    {
        return 'hey';
    }

}

Now, i want to use it properly in my controller like this. echo $this->url->getBaseUri();. But it didn't work.

So, i replaced namespace\class in app/config/services.php from use Phalcon\Mvc\Url as UrlResolver; to use Extension\Phalcon\Mvc\Url as UrlResolver;

But i'm getting this error Fatal error: Class 'Extension\Phalcon\Mvc\Url' not found



58.4k
Accepted
answer

Hi

  • Fatal error: Class 'Extension\Phalcon\Mvc\Url' not found* are you sure loader it


6.2k

Ok this case is my fault that i use wrong namespace. But please show me example how to implement another class (extend class or whatever) that Phalcon said in the document.


Lets begin with Tag class. The tag class can call with chaining method in controller (and view, maybe, i'm not sure).

namespace Extend\Phalcon;

class Tag extends \Phalcon\Tag
{

    public static function linkTo($parameters, $text=null){ 
        return 'no link';
    }

}

I had already add this in app/config/services.php

$di->set('tag', function() {
    $tag = new \Extend\Phalcon\Tag();
    return $tag;
}, true);

I can call $this->tag->linkTo('somewhere', 'go to somewhere'); successfully. But i cannot call to \Phalcon\Tag::linkTo('whereever', 'some where else'); and got no link text.