Question

I am working on a website using the Lithium framework for PHP, and I need to have two sub-directories (ie. for admin and blog) with my controllers and views:

-controllers
  -admin
    HomeController.php
    ...
  -blog
    HomeController.php
    ...
  HomeController.php
  ...

-views
  -admin
    -home
      index.html.php
      ...
    ...
  -blog
    -home
      index.html.php
      ...
    ...
  -layouts
    default.html.php
    admin.html.php
    blog.html.php

So far, I have discovered the way to allow the use of sub-domains in the controller using the Dispach::config() method:

Dispatcher::config(array('rules' => array(
  'admin' => array('controller' => 'app\controllers\admin\{:controller}Controller'),
  'blog' => array('controller' => 'app\controllers\blog\{:controller}Controller'),
)));

This works when you use the following routing:

$options = array('continue' => true);

Router::connect('/admin', array(
  'admin' => true,
  'controller' => 'Home'
), $options);

Router::connect('/admin/{:args}', array(
  'admin' => true
), $options);

Router::connect('/blog', array(
  'blog' => true,
  'controller' => 'Home'
), $options);

Router::connect('/blog/{:args}', array(
  'blog' => true
), $options);

Now the problem I am having is that I can't figure out how to set it to automatically use the admin/blog template and admin/blog view folders.

Was it helpful?

Solution

You could override default templates paths thanks to Media. The filter above sets different paths for admin requests (in config/bootstrap/media.php).

Dispatcher::applyFilter('_callable', function($self, $params, $chain) {
  $next = $chain->next($self, $params, $chain);

  if ($params['request']->admin) {

    Media::type('default', null, array(
        'view' => 'lithium\template\View',
        'paths' => array(
            'layout' => '{:library}/views/layouts/{:layout}.{:type}.php',
            'template' => '{:library}/views/admin/{:controller}/{:template}.{:type}.php'
        )
    ));
  }

return $next;

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top