cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Namespace-related views paths

Open hesalx opened this issue 13 years ago • 11 comments

What about to implement namespace-related views paths? I mean the implementation in the core level, because it could be done by dynamic call of view->pick() but it would be really slower and not pretty at all =)

It would be great to have such structure: /views/name/space/my_controller/my_action.phtml /views/layouts/name/space/my_controller.phtml /views/partials/part.phtml /views/index.phtml

  1. Single general index.phtml (main layer)
  2. Shared partials.
  3. Separated controllers/actions views.
  4. Separated layouts.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

hesalx avatar Jun 06 '13 13:06 hesalx

We've added in 1.2.0 a new event in Phalcon\Mvc\Application to allow the developer to call the render method as he/she needs.

You can create a plugin like this:

<?php

class CustomRenderer extends Phalcon\Mvc\User\Plugin
{
    public function viewRender($event, $application, $view)
    {
      $dispatcher = $this->dispatcher;

      $controllerName = str_replace('\\', DIRECTORY_SEPARATOR, $dispatcher->getNamespaceName()) . 
              DIRECTORY_SEPARATOR . 
          $dispatcher->getControllerName();

      $view->render($controllerName, $dispatcher->getActionName(), $dispatcher->getParams());
    }
}

The plugin must be attached to the application:


$evManager->attach('application:viewRender', new CustomRenderer());

$app = new Phalcon\Mvc\Application($di);

$app->setEventsManager($evManager);

ghost avatar Jun 07 '13 14:06 ghost

I'm using view->pick in beforeExecuteRoute now. Yes, almost everything could be done with events, plugins, overriding and etc. But the main feature of Phalcon is its fast C-based core. Using a lot of PHP for custom purposes (all except an unique application logic) just approximate Phalcon to other php-frameworks. It's not an urgent need, just the wish for improvement.

hesalx avatar Jun 07 '13 14:06 hesalx

The code I'm suggesting you is very small to represent a bottleneck or impact the performance in your application, the amount of overhead we could save writing that small method in C is really minimum. I don't think the argument here is performance.

ghost avatar Jun 07 '13 15:06 ghost

Ok =) I said it's not urgent. You can defer it for a while. While there aren't more important things to do =) It's just my wish and opinion as a developer using Phalcon.

hesalx avatar Jun 07 '13 15:06 hesalx

I think also that this issue is interesting, but in practice it is not often used, so vote -1

iforp avatar Jun 07 '13 15:06 iforp

@Eavu great, all the feedback is very welcome :)

ghost avatar Jun 07 '13 15:06 ghost

I agree this would be wonderful to implement. The MVC samples as well as the other Phalcon apps (Invo, Forum, PHP site) are all very trivial 1-layer controller/action.

I completely disagree with @agent-j. In enterprise, it's very common for PHP applications to have multiple sub-directories or namespaces, and in my opinion, is one feature that I've found lacking as I love Phalcon; however, would love to see some documentation/changes to support enterprise applications that have large organized view/controllers.

For example .. Here is a typical hierarchy. Phalcon documentation does not specify how to properly namespace this with the routes, controllers, and views to achieve this in a single module. I would love to see an additional MVC sample application that had a few 4-5 level namespaced folders with corresponding routing, controllers, and views.

/support/technical-support/operations/tier1-team
/support/technical-support/contact
/support/technical-support/email
/support/technical-support
/support

CONTROLLER \MyApp\Controllers\Support\TechnicalSupport\Operations\Tier1TeamController

VIEWS 1] /views/support/technical-support/operations/tier1-team/index.phtml [ action ] 2] /views/layouts/support/technical-support/operations/tier1-team.phtml [ layout - specific] 3] /views/layouts/support/technical-support/operations.phtml [ layout - general/section ] 4] /views/layouts/support.phtml [ layout - general/support ] 5] /views/index.phtml

Currently, the only way to achieve something similar is to pick the explicit view (in each route or shared per controller in a beforeExecuteRoute) or use a custom renderer as Phalcon mentioned. Regardless of either method, as shown in the above sequence, option #2 and #3, will currently not be used. Layouts should stack upwards using the [ getContents() ] method until no further granular namespace layout exists.

@phalcon You're right .. this feature has nothing to do with performance. Instead, it's with convenience. If namespaces aren't explicitly and automatically handled, it just adds too much complexity from an organizational perspective.

In general, I would recommend the following improvements:

  • Capability to stack the layout components of views (Include line items 3 and 2 above).
  • Inclusion of the namespace path to automatically be used in view rendering (Prevent $view->pick or application:viewRender hook). It could be a simple trigger when registering the View service to use namespace for view path. (e.g. $this->view->useNamespaces(true); )
  • Documentation updates to include a multi-nested directory structure that uses wildcard routing with namespaces.

virgofx avatar Jul 28 '14 23:07 virgofx

This functionality would definitely be appreciated in our environment. We namespace our controllers for organization and proper hierarchical structure but the view module doesn't recognize namespaces, so our views folder has to be "un-namespaced".

nsossonko avatar Oct 20 '15 13:10 nsossonko

I have done using @arantir 's way.

Plugin:

namespace App\Website\Plugins;

class CustomRenderer extends \Phalcon\Mvc\User\Plugin
{
    public function beforeExecuteRoute($event, $dispatcher)
    {
        $defaultNamespace = $dispatcher->getDefaultNamespace();
        $namespace = $dispatcher->getNamespaceName();

        if (strpos($namespace, $defaultNamespace) === 0) {
            $namespace = substr($namespace, strlen($defaultNamespace));
        }

        $parts   = array_filter(explode('\\', $namespace));
        $parts[] = $dispatcher->getControllerName();
        $parts[] = $dispatcher->getActionName();

        $this->view->pick(implode(DIRECTORY_SEPARATOR, $parts));
    }
}

Binding:

$di->set('dispatcher', function() use ($di) {
            $eventsManager = $di->getShared('eventsManager');
            $eventsManager->attach('dispatch', new CustomRenderer()); 

            $dispatcher = new Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            $dispatcher->setDefaultNamespace('App\Website\Controllers'); 
            return $dispatcher;
        }); 

tugrul avatar Mar 05 '16 16:03 tugrul

@tugrul There is an (it seems undocumented) application:viewRender event that might be closer to what you want. In case many dispatches happend before one render, as when $dispatcher->forward is called. The plugin is great, with one tiny downside : the "default namespace" is also becoming the "root namespace". Which totally suits me, maybe other setups would lead to confused situation.

Edit binding it to application:viewRender will overrides any "pick" previously done in the action function. I've reworked it to act on the view->render parameters instead (the pick will be handled inside the render), which appears to be the purpose of the event. Here's what I went with :

use Phalcon\Mvc\User\Plugin as Plugin;
use Phalcon\Events\Event as Event;
use Phalcon\Mvc\Application as Application;
use Phalcon\Mvc\View as View;

class ViewPicker extends Plugin {
  
  public function viewRender(Event $event, Application $application, View $view) {
    
    $dispatcher = $this->getDi()->getShared('dispatcher');
    
    $defaultNamespace = $dispatcher->getDefaultNamespace();
    $namespace = $dispatcher->getNamespaceName();
    
    if (strpos($namespace, $defaultNamespace) === 0) {
      $remainingNamespace = substr($namespace, strlen($defaultNamespace) + 1);
    }
    
    if($remainingNamespace) {
      $parts   = array_filter(explode('\\', $remainingNamespace));
    }
    $parts[] = $dispatcher->getControllerName();
    
    $view->render(
      implode(DIRECTORY_SEPARATOR, $parts),
      $dispatcher->getActionName(),
      $dispatcher->getParams()
    );
    
    return false;
    
  }
  
}

challet avatar Nov 24 '16 19:11 challet

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 16 '18 22:04 stale[bot]