Problems with getBaseUrl() (maybe a bug?)

Hi all,

I have a problem with getting the entry script included in the url for Selenium functional tests. I am using Yii 1.1.10.

I tried Yii::app()->homeUrl which worked in development, but it didn’t work on the live server, because it returned a relative url.

So I used Yii::app()->getBaseUrl(true), which works in development, and also on the live server. However, when testing the application with Selenium, things go wrong.

In my config file test.php I have configured the url manger for Selenium, and set showscriptname to true. The entry script for selenium tests is index-test.php instead of index.php.

According to the documentation, when showscriptname is set to true, getBaseUrl should show the entry script in the url. But it does not!

It might be important to mention I want to use the links in an onclick event, like this:

onclick="window.location.href=’<?php echo Yii::app()->getBaseUrl(true).’/controller/action’; ?>

Other things I tried:

  • Yii::app()->request->baseUrl (does not work either)

  • Yii::app()->request->getBaseUrl(true) (does not work either)

  • Yii::app()->request->getHomeUrl() (does not work on live server)

So, does anyone know how to solve this problem? Or if it’s a bug in Yii? Any help would be appreciated.

Kind regards,

Edwin

According to the getBaseUrl() docs:

Hi Keith, Thanks for your reply.

There is a bit more info in the documentation:

So it can return a relative url as well.

When I use Yii::app()->request->getScriptUrl() the links work in both development and on the live server. However, it now shows index.php in the url after clicking these links, but not when clicking other links. In my config main.php I have set showscriptname to false.

My product owner does not want to see index.php in the url, so I don’t think I am out of the woods yet. I only want (or need) to show the entry script in the url when running selenium.

I’m a little confused, do you still feel there’s a bug with the getBaseUrl() method?

Could you explain why you’re using getBaseUrl() to create links? CHtml::link() and Controller::createUrl() will show the script name as required based on your config.

Well, I don’t know if there is a bug, but if a function does not do what the documentation specifies, something has to be wrong. Could be the documentation also.

I really don’t care what function I have to use to make it work, I just want to create a bug free application that I can test with selenium.

What I did now is I stripped index.php of the url, at least it works, so I can move on.

I did not try CHtml::link() and Controller::createUrl(), nor do I have any time anymore. With so many options to do the same thing, it is me who is getting confused. Maybe after 10 years experience I will get the hang of it.

Too bad you need ten years framework experience first :)


Yii::app()->getBaseUrl(true).'/controller/action'

is wrong.

getBaseUrl DOES NOT return ‘index.php’, because it must be used also for static files, like in:


Yii::app()->getBaseUrl(true).'/css/file.css'

, which would not work with ‘index.php’ in it.

‘true’ means only ‘return absolute url instead of relative to server root’

to create links you must use one of:

  • CController::createUrl (referenced of course by $this->createUrl in views and actions)

  • CHtml::normalizeUrl

  • Yii::app()->urlManager->createUrl

those will follow configuration settings wheter to add script filename (index*.php, request format (path or GET param, etc).

Possible reason for your configuration working in some cases is .htaccess files with mod_rewrite rules which redirected requests to index.php, but they failed for selenium because it uses index-test.php

Hi redguy, thanks for your reply.

I don’t know where I read this, but I can’t seem to find it in the documentation anymore:

Anyway, right now I use Yii::app()->request->getScriptUrl(), it that wrong too? If so, I would consider changing it, but testing this is a b*tch! (selenium tests have to succeed before anything will be pushed to the live server, good thing they pay me by the hour nowadays :))

BTW, at this moment I do not have access to the .htaccess files on the live server, so I cannot check those.

I’d recommend changing all of your links to use one of the proper link generation methods. Personally, I’d use CHtml::link() if all of the links are intended for use on web pages.

Using the proper methods will mean that your links work regardless of the way you’ve configured your urlManager component.

+1 :)