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

Wierd router issue

I have something strange happening. Didn't notice it before.

When I use <a href=""> in my view files the url is as it should be eg: https://site.ext/backend/users or https://site.ext/backend/login

But when I use a redirect within a controller then 'public' is added to the path. Anyone have an idea how to prevent the public from appearing when using a redirect?

  return $this->response->redirect( 'backend' );

This should redirect me to https://site.ext/backend/ but instead it redirects me to https://site.ext/public/backend/ instead. I do not recall having this issue with other phalcon projects.

The site is running on a vagrant vm in a virtualbox environment, with apache and php-fpm. The used .htaccess files are as follows:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ public/         [L]
  RewriteRule ((?s).*) public/$1 [L]
</IfModule>

and

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

Phalcon version 3.3.2 running on php 7.2

php -i
phalcon

Web framework delivered as a C-extension for PHP
phalcon => enabled
Author => Phalcon Team and contributors
Version => 3.3.2
Build Date => Mar 10 2018 14:53:32
Powered by Zephir => Version 0.10.7-2917ebe8ae

Directive => Local Value => Master Value
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.column_renaming => On => On
phalcon.orm.disable_assign_setters => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.enable_literals => On => On
phalcon.orm.events => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.ignore_unknown_columns => Off => Off
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.not_null_validations => On => On
phalcon.orm.update_snapshot_on_save => On => On
phalcon.orm.virtual_foreign_keys => On => On


4.5k
Accepted
answer
edited May '18

Seems I found a solution to my issue. Placing this in the depencency injector solves my problem.

      // Set-up url
      $di->set( 'url', function()
      {
        return (new Url())
          ->setBaseUri( "/" );
      });

Always set base URI using setBaseUri.

I always put that value in a config object:

$url->setBaseUri($config->application->baseUri);