We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

about \Phalcon\Mvc\View setViewsDir can add more directories or can I overrides the \Phalcon\Mvc\View load file method ?

here is situation: I got a website by access diffrently level 3 domain name will response diffently html.

it's will support \Phalcon\Mvc\View setViewsDir can add more directories ?

or

can I overrides the \Phalcon\Mvc\View load file method ?
(I find the document about \Phalcon\Mvc\View , there is not support this. or maybe I just "blind" miss it. )
(if like to normal php framerwork use file then I can edit the source php files. but I want use Phalcon.)

I use dispatcher make transfer to other controller as default namespace.

here is my web directories:

webroot
|
|--...
|--controller
    |--IndexController(Controller\)
|--private
    |--10000
        |--control
            |--IndexController(Controller\Privater)
        |--view(I want some diffent level 3 domain name got there own view html files if it's not exeists in the webroot\view directory  )
|--public
|--view

example:

<?php

use
    ...
    ,\Phalcon\Loader
    ...
    ,\Phalcon\Mvc\Dispatcher
    ...
    ;

class Application extends Phalcon\Mvc\Application
{
    public function main()
    {

        (new Loader())->registerNamespaces(
            [
                'Controller' => '../controller/',
                'Model'      => '../model/'
            ]
        )->register();

        ...

        $di->set('dispatcher', function () {
            $dispatcher = new Dispatcher();

            $dispatcher->setDefaultNamespace('Controller\\');

            return $dispatcher;
        },true);

        ...

        $serverNameArray = explode(".",$this->request->getServerName());

        if (file_exists("../private/{$serverNameArray[0]}/")){
            (new Loader())->registerNamespaces(
                [
                    "Controller\\Privater" => "../private/{$serverNameArray[0]}/controller",
                ]
            )->register();

            $this->dispatcher->setDefaultNamespace("Controller\\Privater");
        }

       ...

    }
}

try {
    $application = new Application();
    $application->main();
} catch (\Exception $e) {
    echo $e->getMessage();
}

Hi,

You can set the path to view folder by this code:


<?php

//Registering the view component
        $di->set('view', function() {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir('../app/backend/views/');
            return $view;
        });

I know it, but it only support one directory.I want use two dictories.

Hi,

You can set the path to view folder by this code:


<?php

//Registering the view component
      $di->set('view', function() {
          $view = new \Phalcon\Mvc\View();
          $view->setViewsDir('../app/backend/views/');
          return $view;
      });


43.9k
edited Mar '17

Hi,

does that code


        $di->set('dispatcher', function () {
            $dispatcher = new Dispatcher();

            $dispatcher->setDefaultNamespace('Controller\\');

            return $dispatcher;
        },true);

        $serverNameArray = explode(".",$this->request->getServerName());

        if (file_exists("../private/{$serverNameArray[0]}/")){
            (new Loader())->registerNamespaces(
                [
                    "Controller\\Privater" => "../private/{$serverNameArray[0]}/controller",
                ]
            )->register();

            $this->dispatcher->setDefaultNamespace("Controller\\Privater");

already work ?

if yes, I think that you will be able to use the same approach to load the view component in the DI. and use webroot/view as a fallback and if it does make sense in your logic as a common layout dir for all your domains ...

edited Mar '17

are u the cphalcon source code commiter? can u explain this?

https://github.com/phalcon/cphalcon/issues/12718

the \Phalcon\Mvc\View load file by iteration?

when I access url : /index/index.

the \Phalcon\Mvc\View will use "webroot/view/index/index.volt" and "webroot/view/index.volt".

when "webroot/view/index.volt" doesn't exists and "webroot/view/index/index.volt" exists it will use "webroot/view/index/index.volt".

when "webroot/view/index.volt" exists and "webroot/view/index/index.volt" exists. it will use "webroot/view/index.volt".

it maybe can solve some problem but it will fired (new EventsManager())->attach("view:notFoundView"), and it's confuse.

the matter is the \Phalcon\Mvc\View load file work will fire (new EventsManager())->attach("view:notFoundView")

Hi,

does that code


       $di->set('dispatcher', function () {
           $dispatcher = new Dispatcher();

           $dispatcher->setDefaultNamespace('Controller\\');

           return $dispatcher;
       },true);

       $serverNameArray = explode(".",$this->request->getServerName());

       if (file_exists("../private/{$serverNameArray[0]}/")){
           (new Loader())->registerNamespaces(
               [
                   "Controller\\Privater" => "../private/{$serverNameArray[0]}/controller",
               ]
           )->register();

           $this->dispatcher->setDefaultNamespace("Controller\\Privater");

already work ?

if yes, I think that you will be able to use the same approach to load the view component in the DI. and use webroot/view as a fallback and if it does make sense in your logic as a common layout dir for all your domains ...



43.9k
Accepted
answer

Hi, as ansewered on github, this is normal hierarchical view rendering.

app/views/index.volt usually only contains some basic html headers such as title (wich can be dynamically changed, see: https://docs.phalcon.io/en/latest/reference/tags.html#changing-dynamically-the-document-title)

What I was trying to explain is that once you've resolved the server name, you can register the appropriate view file (and also the layout directory, usefull if some of your websites share the same layout, see https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_View.html, see setLayoutsDir method).