2

Есть такая структура проекта на Phalcon:

Структура проекта

Нужно сделать правильный роутинг, чтобы например если переходишь по site.ru/profile/1, показывались данные с id = 1 (name, age...). Если переходишь по site.ru/profile/1/edit, то можешь редактировать свой профиль, соответственно учитывая сессию или куки. Если переходишь просто по site.ru/profile, то из данных о сессии перебрасывало на личный аккаунт юзера (например: site.ru/profile/5). Как и каким способом лучше прописать всё это?

2 ответа 2

2

Как бы делал я... Создаем config/routes.php, потом в config/services.php пишем:

$di['router'] = function () {
    return require __DIR__ . '/routes.php';
};

А в config/routes.php пишем:

use Phalcon\Mvc\Router;

$router = new Router();

$router->setDefaults([
    'namespace' => 'Controller',
    'controller' => 'index',
    'action' => 'index',
]);

$profileRouter = new Router\Group([
    'namespace' => 'Controller\\Profile',
    'controller' => 'profile',
]);

$profileRouter->setPrefix('/profile');

$profileRouter->addGet('/', [
    'action' => 'index',
]);

$profileRouter->addGet('/{id:[0-9]+}', [
    'action' => 'view',
]);

$profileRouter->addGet('/{id:[0-9]+}/edit', [
    'action' => 'edit',
]);

$profileRouter->addGet('/{id:[0-9]+}/delete', [
    'action' => 'delete',
]);

$profileRouter->addPost('/save', [
    'action' => 'save',
]);

$router->mount($profileRouter);

return $router;

Это для обычного сайта. Если предполагается несколько контроллеров CRUD с одинаковой структурой, то можно засунуть в цикл:

use Phalcon\Mvc\Router;

$router = new Router();

$router->setDefaults([
    'namespace' => 'Controller',
    'controller' => 'index',
    'action' => 'index',
]);

$groups = [
    '/profile' => [
        'namespace' => 'Controller\\Profile',
        'controller' => 'profile',
    ],
    '/products' => [
        'namespace' => 'Controller\\Products',
        'controller' => 'products',
    ],
    '/categories' => [
        'namespace' => 'Controller\\Categories',
        'controller' => 'categories',
    ],
];

foreach ($groups as $prefix => $groupDefinition) {
    $groupRouter = new Router\Group($groupDefinition);

    $groupRouter->setPrefix($prefix);

    $groupRouter->addGet('/', [
        'action' => 'index',
    ]);

    $groupRouter->addGet('/{id:[0-9]+}', [
        'action' => 'view',
    ]);

    $groupRouter->addGet('/{id:[0-9]+}/edit', [
        'action' => 'edit',
    ]);

    $groupRouter->addGet('/{id:[0-9]+}/delete', [
        'action' => 'delete',
    ]);

    $groupRouter->addPost('/save', [
        'action' => 'save',
    ]);

    $router->mount($groupRouter);
}

return $router;

Если же собираешься делать RESTful API, то немного меняем методы:

use Phalcon\Mvc\Router;

$router = new Router();

$router->setDefaults([
    'namespace' => 'Controller',
    'controller' => 'index',
    'action' => 'index',
]);

$groups = [
    '/profile' => [
        'namespace' => 'Controller\\Profile',
        'controller' => 'profile',
    ],
    '/products' => [
        'namespace' => 'Controller\\Products',
        'controller' => 'products',
    ],
    '/categories' => [
        'namespace' => 'Controller\\Categories',
        'controller' => 'categories',
    ],
];

foreach ($groups as $prefix => $groupDefinition) {
    $groupRouter = new Router\Group($groupDefinition);

    $groupRouter->setPrefix($prefix);

    $groupRouter->addGet('/', [
        'action' => 'index',
    ]);

    $groupRouter->addGet('/{id:[0-9]+}', [
        'action' => 'view',
    ]);

    $groupRouter->addPut('/{id:[0-9]+}', [
        'action' => 'edit',
    ]);

    $groupRouter->addDelete('/{id:[0-9]+}', [
        'action' => 'delete',
    ]);

    $groupRouter->addPost('/', [
        'action' => 'create',
    ]);

    $router->mount($groupRouter);
}

return $router;
1

Это можно сделать только одним способом -- отключить автоматическую генерацию роутинга и создавать правила самостоятельно.

https://docs.phalconphp.com/ru/latest/reference/routing.html#id5

Ваш ответ

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

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