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

Base URI config not showing in file inclusion and link

Hi,

I've got problems using baseUri (or maybe I'm searching in the wrong place) in a config file. I use the phalcon-devtools to generate the project and files.

As I am working on WAMP, my project's url is localhost/myproject.


The problem

Here is my problem, I'm trying to put some CSS / JS or some link in the volt files like this :

{{ stylesheet_link('css/bootstrap/bootstrap.min.css') }}
{{ link_to('test', 'test') }}
{{ javascript_include('js/jquery.min.js') }}

But the generated HTML is like this, with all the link with /public/ :

<link rel="stylesheet" type="text/css" href="/public/css/bootstrap/bootstrap.min.css">
<script type="text/javascript" src="/public/js/jquery.min.js"></script>
<a href="/public/test">test</a>

But I was expecting to have /myproject/public/ like this :

<link rel="stylesheet" type="text/css" href="/myproject/public/css/bootstrap/bootstrap.min.css">
<script type="text/javascript" src="/myproject/public/js/jquery.min.js"></script>
<a href="/myproject/public/test">test</a>

The files

In my config.ini, I've got :

[application]
controllersDir = ../app/controllers/
modelsDir = ../app/models/
viewsDir = ../app/views/
pluginsDir = ../app/plugins/
libraryDir = ../app/library/
cacheDir = ../app/cache/
baseUri = /myproject/

In my services, I've got this, the load of the url component :

use Phalcon\Mvc\Url as UrlResolver;

$di->set('url', function () use ($config) {
  $url = new UrlResolver();
  $url->setBaseUri($config->application->baseUri);
  return $url;
});

Here is my .htaccess :

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

Any help would be welcome,

Thanks.

Have seen a couple other posts with the same issue. Seems like something with htaccess on WAMP. Another post solved by adding the prefix before config uri in url di setup



2.5k

Thanks for your answer. I've seen some posts to with the same issue (like this one), in the di setup, to solve the issue, they use something like :

$di->set('url', function(){
  $url = new \Phalcon\Mvc\Url();
  $url->setBaseUri('/tutorial/');
  return $url;
});

The main difference with my code is $url->setBaseUri('/tutorial/');, I'm using $url->setBaseUri($config->application->baseUri); which value is well setted, as I see when I do a var_dump on it.

ps: I edited my previous post to add my .htaccess

Hi, there ! You can try this

<link rel="stylesheet" type="text/css" href="{{ url("css/bootstrap/bootstrap.min.css") }}">

<script type="text/javascript" src="{{ url("js/jquery.min.js")}}"></script>



2.5k

Hi, thanks for your help Phi Nguyen, it works well.
I will use this as a temporary fix, but I still looking why stylesheet_link, javascript_include and link_to.
If I get some news, I will post it here.



2.5k
Accepted
answer

Here comes the facepalm, I found what causes my problem.

Here is (was) my public/index.php, take a look at the line 8 at the instantiation of \Phalcon\DI\FactoryDefault :

// ... some code

$config = new \Phalcon\Config\Adapter\Ini(APP_PATH . '/app/config/config.ini');

$debug = new \Phalcon\Debug();
$debug->listen();

$di = new \Phalcon\DI\FactoryDefault(); // PROBLEM: Here it is

// ... some code

/**
 * Read services
 */
include APP_PATH . '/app/config/services.php';

// ... some code

This instantiation at line 8 was a relic of a previous test.

In my services.php, I have another instantiation of DI\FactoryDefault.
So I delete the one in the index.php and the url produce by stylesheet_link, link_to, ... becomes valid (with /project/ before).

Apparently, instanciate several times DI\FactoryDefault is not a good thing to do.
These seems logic, but if you know why two instance of DI\FactoryDefault break the use of some tag function, you are welcome.

Anyway, thanks for your help Shad Mickelberry and Phi Nguyen, I appreciate it.