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

Custom Element Error

Hi guys my custom element is not working it gives me an error

Fatal error: Call to undefined method Phalcon\Mvc\User\Component::SetMeta() in C:\Users\spilagan20140973\Desktop\BOOTSTRAP FILES\PORTABLES\USBWebserver v8.6\root\PHALCON\app\cache\c__users_spilagan20140973_desktop_bootstrap files_portables_usbwebserver v8.6_root_phalcon_app_views_index.volt.php on line 12

here is my code

my index.php

<!DOCTYPE html>
<!--[if lt IE 7 ]><html lang="en" class="no-js ie6"><![endif]-->
<!--[if IE 7 ]><html lang="en" class="no-js ie7"><![endif]-->
<!--[if IE 8 ]><html lang="en" class="no-js ie8"><![endif]-->
<!--[if IE 9 ]><html lang="en" class="no-js ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]-->
<head>
    {{ get_title() }}
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    {{ metaelements.SetMeta() }}

    {{ assets.outputCss('header_assets') }}
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

    <section class="container-fluid container-main">

        {{ content() }}

    </section>

    {{ assets.outputJs('footer_assets') }}

</body>
</html> 

services.php

<?php
use Phalcon\Mvc\User\Component as Elements;
$di->set('metaelements', function(){
    return new Elements();
});

/*
* Register Top Navigation Elements
*/
$di->set('topnavelements', function(){
    return new Elements();
});

/*
* Register bottom Navigation Elements
*/
$di->set('bottomnavelements', function(){
    return new Elements();
});

loader.php

<?php

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->libraryDir,
        $config->application->pluginsDir
    )
)->register();

Metaelements.php on my library folder

<?php

use Phalcon\Mvc\User\Component as Elements;

class Metaelements extends Elements
{

    public function SetMeta(){

        echo "meta tag";
    }

}


58.4k
Accepted
answer
edited Nov '14

you try this


    {{ metaelements.SetMeta() }} tobe 
    {{ this. metaelements.SetMeta() }}

So you loader is registerDirs so remove "use Phalcon\Mvc\User\Component as Elements;" in file service.php

    <?php
    $di->set('Metaelements', function(){
        return new Elements();
    });

thnks @Thiện big help :)