1

I'm a newbie at Phalcon programming, I have admin/backend and frontend controllers Admin will be served at '/admin/:controller/:action' and frontend will be served at '/:controller/:action'

Admin controllers (KalkuRitel\Controllers\Admin namespace) are located under

app/
   controllers/
     admin/

and frontend controllers (KalkuRitel\Controllers\Frontend namespace) are located under

app/
   controllers/
     frontend/

How do I accomplish this? And how to serve 404 page within admin and frontend controllers with their own layout?

ir1keren
  • 37
  • 9

2 Answers2

1

I would recommend to create modules

app/
  modules/
    admin/
      ...
    frontend/
      ...
    api/
      ...

register modules:

$application->registerModules(array(
    'frontend' => array(
        'className' => 'Application\Frontend\Module',
        'path' => __DIR__ . '/../modules/frontend/Module.php'
    ),
    'admin' => array(
        'className' => 'Application\Admin\Module',
        'path' => __DIR__ . '/../modules/admin/Module.php'
    ),
    'api' => array(
        'className' => 'Application\Api\Module',
        'path' => __DIR__ . '/../modules/api/Module.php'
    )
));

define properly Module.php files and than set route somewhat close to this:

use Phalcon\Mvc\Router as Router;
use Phalcon\CLI\Router as CliRouter;

/**
 * Registering a router
 */
$di->setShared('router', function () use ($application, $config) {

    if($application instanceof Phalcon\CLI\Console) {
        return new CliRouter();
    }

    $router = new Router(false);

    $router->setDefaultModule("frontend");
    $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_GET_URL);
    $router->removeExtraSlashes(TRUE);

    foreach($application->getModules() as $key => $module) {

        $prefix = $key == 'frontend' ? '' : ('/' . $key);

        $router->add($prefix . '/:params', array(
            'module'     => $key,
            'controller' => 'index',
            'action'     => 'index',
            'params'     => 1
        ))->setName($key);

        $router->add($prefix . '/:controller/:params', array(
            'module'     => $key,
            'controller' => 1,
            'action'     => 'index',
            'params'     => 2
        ));

        $router->add($prefix . '/:controller/:action/:params', array(
            'module'     => $key,
            'controller' => 1,
            'action'     => 2,
            'params'     => 3
        ));

    }


    return $router;
});

More in manual: https://docs.phalconphp.com/pl/latest/reference/routing.html and https://docs.phalconphp.com/pl/latest/reference/applications.html

yergo
  • 4,761
  • 2
  • 19
  • 41
0

The best way to accomplish this is to create a multiple module application.

You can learn more about multiple module application setups here: https://docs.phalconphp.com/en/latest/reference/applications.html#multi-module

Once you have the structure for this in place you can set the routes something like this:

/*
* Dependency Injector
*/
$di = new \Phalcon\Di\FactoryDefault();

/**
* Register a router
*/
$di->setShared('router', function () {
  $router = new \Phalcon\Mvc\Router();

  $router->setDefaultModule('frontend');
  $router->setDefaultNamespace('Multiple\Frontend\Controllers');

  /*
  * Frontend Routes
  */
  $router->add('/:controller/:action', array(
    'namespace'  => 'Multiple\Frontend\Controllers',
    'module'     => 'frontend',
    'controller' => 1,
    'action'     => 2
  )); 

  /*
  * Backend Routes
  */     
  $backendRoutes = new \Phalcon\Mvc\Router\Group(array(
    'namespace' => 'Multiple\Backend\Controllers',
    'module'    => 'backend'
  ));
  $backendRoutes->setPrefix('/admin');

  $backendRoutes->add('/:controller/:action', array(
    'controller' => 1,
    'action'     => 3
  ));

  $router->mount($backendRoutes);

  return $router;
});

This is a quick answer and may require tweaking for your individual situation but should give you a good idea of how to accomplish your goal.

Armon Bigham
  • 339
  • 2
  • 14