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

Unable to call Index controller's actions

Hi everyone. I have a simple project in my xampp/htdocs directory called phalcon and i have apache configured to point to that folder so that i can go to phalcon/ in my browser. The problem occurs when I try to open an index controller view other than index(default). For instance I have someAction in Index controller and in views/index I have some.phtml. If I go to phalcon/index/some I don't get the text from some.phtml outputed to the page. It's probably because it thinks as if I wan't to open IndexController->indexAction and pass some as a parameter. Would be grateful on any help provided to resolve this. P.S. the project skeleton was copied from https://github.com/phalcon/skeleton-single

edited Apr '14

Do you get no output at all?

If you call

exit('test')

from your someAction method, do you get anything?

Can you post the code for your controller?



28.4k

No, if I go to phalcon/index/some and I have exit('test') in some action nothing gets displayed. Here is a project folder.Here is a link : https://www.dropbox.com/s/b5tbdqq7l0i9fx1/phalcon.7z

Please just copy & paste your controller code here - so others that may be having this problem in the future can also maybe benefit. Please read: https://forum.phalcon.io/help/markdown for how to display code.

In your bootstrap file, do a dump of $_SERVER, to see what the requested URL actually is. It's possible your URL rewrites aren't working.



28.4k
edited Apr '14

Index controller:

    <?php

    class IndexController extends ControllerBase
    {

        public function indexAction($action = null)
        {

      }
      public function someAction () {
          exit('test');
      }

    }

views/index/index.phtml :

        <?php echo $this->getContent(); ?>

views/index/some.phtml:

Some Action

views/index.phtml

   <!DOCTYPE html>
    <html>
    <head>
        <title>Phalcon PHP Framework</title>
    </head>
    <body>
        <?php echo $this->getContent(); ?>
    </body>
    </html>
edited Apr '14

Is ControllerBase setting something in the view? I don't personally use getContent() so I'm not really familiar with how it works.

I believe you can set a beforeDispatch handler and output the controller and action that Phalcon has determined to use.



28.4k

What do you use instead of getContent?



28.4k

Controller Base is empty in only extends Phalcon/Mvc/Controller



28.4k

Controller Base is empty in only extends Phalcon/Mvc/Controller

Nothing - I just use Volt syntax, and don't have any inherited templates. So my view/index.phtml file would look something like:

{{ partial('partials/head.phtml') }}
Content specific to this page
{{ partial('partials/foot.phtml') }}

There's nothing wrong with using getContent() - it may even be superior to my method.



28.4k

If I go to any other controller that I created and any other action everything displays correctly. Maybe it has something to do with my base uri? I have it set to 'https://phalcon/'

I'm not so sure. I believe that URL component just has to do with generating URLs in the view - it isn't a system wide setting.

I just re-read your initial post and I think I know what the problem is. By default, a Phalcon URL will have a controller component, and an action component, like: www.mydomain.com/book/buy.

The index controller is only called when the URL has no controller or action component, like: www.mydomain.com/. Other actions in the index controller - like your some won't ever get accessed because what would the URL to them look like: www.mydomain.com//some? That's a nonsensical URL.

If you want your some action to be called, like with this URL: www.domain.com/some, then you should put that code in a someController in the indexAction method.



28.4k

Ok. Thanks, but isn't IndexController get called when you explicitly call it like for instance www.domain.com/index/actionName ?

I don't know. Honestly I've never tried that. If all of your other controllers and actions are working except non-index actions on the index controller, then I would hazard to guess not.



28.4k

Here, I have asked a same question a couple days ago and I guy said me that everything worked at his project and posted these photos : https://vk.com/topic-45934290_28737621?z=photo-45934290_327264411%2Fpost-45934290_642

https://vk.com/topic-45934290_28737621?z=photo-45934290_327264444%2Fpost-45934290_642

edited Apr '14

Oh. Well evidently I'm not familiar enough with this to be of much help.



28.4k

Ok. Thank you for everything you've done anyway)



26.3k

@angay9 HI!

Let's go to the file file/config/config.php as here https://github.com/phalcon/skeleton-single/blob/master/app/config/config.php

There is this config array here. The last element of the array is baseUri. Have you modyfied it? I think this might be a case. I am not sure what the value you should have but definitly not /single-skeleton/. I think it shoul be / but i am not sure. Maybe /phalcon/? Or maybe in your case this element should be deleted at all?



28.4k

I have changed but I have tried different variants including default one and it still doesn't work.

Try to place die(); in IndexController

class IndexController extends ControllerBase {

public function indexAction()
{
    echo "<h1>Hello</h1>";
    die();
}

}

php is parsing through the controller but in the last because of the view the ouput shows you the view only.



841
edited Dec '14

@angay9

Using the Phalcon Tools I have created an app. No problems. Created some controllers and models and views and everything works as expected except accessing the indexController's methods. Sorry to dig up an old thread, but did you ever find the solution ?

If I use the following request specifying the paramater name:

https://example/index.php?_url=/index/method/argname

everything works. Whereas :

https://example/index/method/argname

does not. Like you this does work :

https://example/test/method/argname

I cannot see anything to cause a redirect route. If you know any more or anyone else can shed light, it would be appreciated.



841
edited Dec '14

I dumped the $_SERVER var from public/index.php and noticed differences from the successful test route and the failing index route.

The successful one showed:

[REDIRECT_STATUS] => 200

[QUERY_STRING] => _url=/test/metho/argname

[PHP_SELF] => /index.php

The failed index route had no [REDIRECT_STATUS] and [QUERY_STRING] => ( empty )

It also showed :

[PATH_TRANSLATED] => redirect:/index.php/argname

[PHP_SELF] => /index.php/metho/argname

So off to apache and check the VirtualHost.

I added Options All as per the documentation :

https://docs.phalcon.io/en/latest/reference/apache.html

And success. IndexController methods are called.

My Apache httpd.conf sets Options to :

Options FollowSymLinks Multiviews

Options All - enables all settings other than MultiViews. Removing MulltiViews would, if you did not have a VirtualHost Directory entry, also work.

Further, if you disable all Options settings other than FollowSymLinks, all works. So this in your Phalcon VirtualHost Directory works :

Options +FollowSymLinks -ExecCGI -Includes -IncludesNOEXEC -Indexes -MultiViews -SymLinksIfOwnerMatch

but may not be what you want.

I was remiss in not reading the docs (again ?). Anyhow, I hope this helps someone.