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

Disabling views

Hi Community!

As a student i was wondering how i can block certain views. It is possible to open them by URL but i want the views only to open by action. I read the documentation about disabling views but i find it hard to understand how it actually works.

Hopefully some of you can help me out!

What you mean to only open by action? Action is assosciated with URL. You can disable whole view component in action or just certain view level, everything is in docks.



1.8k

I think i should have explained it differently. My thing is, when i just type in the url in the browser it opens too. I do not want this to happen. I want this view only opens by the action in the controller. See i want it to be a protected area only accessed by action.

hopefully this explains my question, thanks for the reply.

What you mean to only open by action? Action is assosciated with URL. You can disable whole view component in action or just certain view level, everything is in docks.

Then just move your directory with views, controller and php outside of public folder and in webserver configuration forward all urls to public index.php.



1.8k

Hi thanks again

This helped for a part. Now if i type in the url it will not load the view which i wanted. I changed te rewriterule in the .htaccess:

From: *RewriteRule ^(.)$ index.php?q=$1 [L,QSA]**

To: RewriteRule . index.php [L]

Now in my loginController:

public function loginAction()
{
    return $this->dispatcher->forward(
                [
                    "controller" => "admin",
                    "action" => "index",
                ]
            );
}

in my adminController:

public function indexAction()
{
    $this->view->render("admin", "index");
}

It does not render the view. Which i do want from this action. What am i doing wrong?

Then just move your directory with views, controller and php outside of public folder and in webserver configuration forward all urls to public index.php.

edited May '17

Hi, disabling views in Phalcon works very easy.

When you write in browser url like www.abc.com/users/adduser you get the view adduser.phtml. Right?

The action associated with this URL is located in controller UsersController and named


adduserAction(){
//your code which is executed in this action
}

But, the view that is located one step upper for ex. index.phtml is showed aswell, because Phalcon has hierarchical rendering

hierarchical-rendering

So if you want to render only adduser.phtml without any other views, just disable the for ex. Main View by


...

use Phalcon\Mvc\View;

...

class UsersController extends \Phalcon\Mvc\Controller
{

...

    adduserAction(){
    $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
    //your code which is executed in this action
    }

...
}

This should help: https://github.com/phalcon/mvc/tree/master/simple

Or, you should post your folder structure so we can help.



1.8k
edited May '17

Hi thanks for the replies.

This is my map structure of my views:

/Views /admin /index.volt

/article
    /index.volt
    /editArticle.volt
    /newArticle.volt
    /readArticle.volt

/index
    /index.volt

/login
    /index.volt
    /loginFailed.volt

Controllers are located in app/controllers



1.8k

Hi,

I posted my map structure hopefully that helps. I will read the docs as well and try a bit in the mean time thanks for your explanation.

Hi, disabling views in Phalcon works very easy.

When you write in browser url like www.abc.com/users/adduser you get the view adduser.phtml. Right?

The action associated with this URL is located in controller UsersController and named


adduserAction(){
//your code which is executed in this action
}

But, the view that is located one step upper for ex. index.phtml is showed aswell, because Phalcon has hierarchical rendering

hierarchical-rendering

So if you want to render only adduser.phtml without any other views, just disable the for ex. Main View by


...

use Phalcon\Mvc\View;

...

class UsersController extends \Phalcon\Mvc\Controller
{

...

  adduserAction(){
  $this->view->disableLevel(View::LEVEL_MAIN_LAYOUT);
  //your code which is executed in this action
  }

...
}


1.8k
Accepted
answer

Hi all,

I found the solution. I was able to use session data to get the result i wanted.

Thanks for the replies, i was just looking in the wrong direction.