0

Подскажите. На данный момент Роутинг работает по принципу /контроллер/действие/параметры Хочу добавить свой роутинг. Вот index.php

error_reporting(E_ALL);


use Phalcon\Exception;
use Phalcon\Loader;
use Phalcon\Di\FactoryDefault;
use Phalcon\Url;
use Phalcon\Db\Adapter\Pdo\Mysql;

use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Router;

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

define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// ...

$loader = new Loader();

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

$loader->register();

// Create a DI
$container = new FactoryDefault();

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


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

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


$container->set(
    'Router',
    function () {
        // Create the router
        $router = new Router();
        
        // Define a route
        $router->add(
            '/posts',
            [
                'controller' => 'index',
                'action'     => 'posts',
            ]
            );
        
        return $router;
    }
    );


$application = new Application($container);

try {
    // Handle the request
    $response = $application->handle(
        $_SERVER["REQUEST_URI"]
        );
    
    $response->send();
} catch (Exception $e) {
    echo 'Exception: ', $e->getMessage();
}
?>  

при переходе по http://localhost/posts

Exception: PostsController handler class cannot be loaded Что я делаю не так

1 ответ 1

0

Сработало следующее

$container->set(
    'router',
    function(){
        $router = new \Phalcon\Mvc\Router(false);
        $router->removeExtraSlashes(true);
        
        $router->add('/posts',
            array(
                'controller' => 'index',
                'action' =>'posts'
                
            ));
        
        return $router;
    }); 

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.