N
N
nowaybuticanfly2021-02-26 09:24:03
Phalcon
nowaybuticanfly, 2021-02-26 09:24:03

How to set up namespaces in Falcon?

Through registerDirs, the autoloader loads everything correctly, through namespaces it gives a dispatch error, I can’t understand what the problem is, the path is set to the same

index.php code

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
use Phalcon\Db\Adapter\Pdo\Mysql;

// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

// Register an autoloader
$loader = new Loader();

//$loader->registerDirs(
//    [
//        APP_PATH . '/controllers/',
//        APP_PATH . '/models/',
//        APP_PATH . '/modules/'
//    ]
//);

$loader->registerNamespaces(
    [
     'App\Controllers' =>   APP_PATH . '/controllers/'
    ]
);

$loader->register();


$container = new FactoryDefault();

$container->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir(APP_PATH . '/views/');
        return $view;
    }
);

$container->set(
    'db',
    function () {
        return new Mysql(
            [
                'host'     => 'localhost',
                'username' => 'sammy',
                'password' => 'password',
                'dbname'   => 'learning',
            ]
        );
    }
);

$container->set(
    'url',
    function () {
        $url = new Url();
        $url->setBaseUri('/');
        return $url;
    }
);

$application = new Application($container);


    // Handle the request
    $response = $application->handle(
        $_SERVER["REQUEST_URI"]
    );

    $response->send();


controller code

<?php
declare(strict_types=1);
namespace App\Controllers;

use Phalcon\Http\Request;
use \Phalcon\Mvc\Controller;
use Phalcon\Http\Message\Uri;

class IndexController extends Controller
{

    public function indexAction()
    {
        return 'hi';
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
My joy, 2021-02-26
@nowaybuticanfly

This is how it works for me:

$loader = new Loader();

$loader->registerDirs([
    '../app/Controllers/',
    '../app/Models/',
    '../app/Libs/',
]);

$loader->registerNamespaces([
  'App' => '../app/',
]);

$loader->registerFiles([ '../vendor/autoload.php' ]);

$loader->register();

subject to the folder structure:
2 folders at the same level pub and app. index.php file (in which the code above) is in pub/ all logic is in app/
pub/ (index.php)
app/ (controllers/, models/, etc)

And this is an example of the controller itself
<?php

  /**
  *	Офис.Справка
  */
  namespace App\Controllers\Office;
  
  use App\Models\Faq;
  use App\Models\FaqCats;
  use Phalcon\DI;
  
  class FaqController extends BaseController {

    public function initialize() {
      
      parent::initialize();
      
    }
      ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question