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

Can't load any external libraries

So i can't load any external libraries , mainly because there is no good walk trough . Any Help ? I'm working with Multi Modules, so i have the structure :

apps
-- frontend/
    -- config/
    -- controllers/
    -- models/
    -- views/
    -- Module.php

-- backend/
    -- config/
    -- controllers/
    -- models/
    -- views/
    -- Module.php

config
-- services.php
-- modules.php

public
-- assets/
-- .htaccess
-- index.php


43.9k

Hi,

can't see any "library" directory in your app structure. So you should first create a library directory inside your root app's directory and put there your third party libraries (ex phalcon's incubator. I recommend you to use the built in loader to register your libaries as namespaced as it is explained in the incubator's README or in the official doc

okay then , say that i made the folder and put the files in it , how do i use them?



43.9k

<?php

// Creates the autoloader in you bootstrap file
$loader = new \Phalcon\Loader();

//Register some namespaces
$loader->registerNamespaces(
    array(
       "Example\Base"    => "public/example/base/",
       "Example\Adapter" => "public/example/adapter/",
       "Example"         => "public/example/",
    )
);

// register autoloader
$loader->register();

// elswhere in your app
// The required class will automatically include the
// file vendor/example/adapter/Some.php
$some = new Example\Adapter\Some();


16.4k

Let me know if this works

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerNamespaces(array(
      "Example\Base"    => "public/example/base/",
       "Example\Adapter" => "public/example/adapter/",
       "Example"         => "public/example/",
        'Guzzle' => '/home/library/guzzle/guzzle/src/Guzzle/', //externa library
));

$loader->register();

require_once __DIR__ . '/../vendor/autoload.php'; //composer

namespace Frontend;

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Config\Adapter\Ini;

class Module implements ModuleDefinitionInterface
{
#    Registers the module auto-loader
public function registerAutoloaders()
{

    $loader = new Loader();

    $loader->registerNamespaces(array(
        'Frontend\Controllers'  => __DIR__ . '/controllers/',
        'Frontend\Models'       => __DIR__ . '/models/'
    ));

    $loader->register();
}

/**
 * Registers the module-only services
 *
 * @param Phalcon\DI $di
 */
public function registerServices($di)
{

    /**
     * Read configuration
     */
    $config = new Ini(__DIR__ . "/config/config.ini");

    /**
     * Setting up the view component
     */
    $di['view'] = function () {
        $view = new View();
        $view->setViewsDir(__DIR__ . '/views/');

        return $view;
    };

    /**
     * Database connection is created based in the parameters defined in the configuration file
     */
    $di['db'] = function () use ($config) {
        return new DbAdapter(array(
            "host" => $config->database->host,
            "username" => $config->database->username,
            "password" => $config->database->password,
            "dbname" => $config->database->dbname,
            "charset" => $config->database->charset
        ));
    };

}

}