This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Access symfony project using Apache Alias

Installation and environment issues, mainly on 1.x but there's some 2.x here too.

Access symfony project using Apache Alias

by xiblack » Tue Oct 25, 2005 8:04 am

Hi,

In the default project folder (generated with 'symfony init-project'), I found this file: config/apache.conf

Code: Select all
<VirtualHost *:80>
  ServerName SERVERNAME
  DocumentRoot "/var/www/sfTest/web"
  Alias /sf ##SYMFONY_DATA_WEB_SF##
  <Directory "/var/www/sfTest/web">
    AllowOverride All
  </Directory>
  ErrorLog /var/log/httpd/sfTest-error_log
  CustomLog /var/log/log/sfTest-access_log common
</VirtualHost>


From symfony documentations, this configuration is an example to config apache server to run symfony project using virtualhost.
I have a question: can I setup symfony project using Alias?
The configuration may looks like this:
Code: Select all
Alias /sfTest/ "/var/www/sfTest/web/"

<Directory "/var/www/sfTest/web">
    DirectoryIndex index.php
    AllowOverride All
</Directory>


Indeed, I configured this way and I got the Congratulation page when I access http://localhost/sfTest/index.php

However, I saw this error in Apache error-log keep raising up:
File does not exist: /var/www/css, referer: http://localhost/sfTest/index.php

And I also can not access from this url: http://localhost/sfTest/ (return Bad Request - Invalid URI in request GET /sfTest/ HTTP/1.1)


Please help,
Thanks,
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by xiblack » Wed Oct 26, 2005 4:26 am

After some search, I find out the function that return the path to stylesheet and javascript files. It's in
Code: Select all
SF_SYMFONY_LIB_DIR . 'symfony/helper/AssetHelper.php'


the function is '_compute_public_path'

Code: Select all
function _compute_public_path($source, $dir, $ext)
  {
    if (strpos($source, '/') === false) $source = '/'.$dir.'/'.$source;
    if (strpos($source, '.') === false) $source = $source.'.'.$ext;

//$source = "#{@request.relative_url_root}#{source}"
    return $source;
  }


There is nothing wrong with this function if the symfony project is hosted in the root folder (i.e. http://www.example.com/) because the return path is '/js/somejs.js' or '/css/somecss.css'. However, if the symfony project is hosted under an alias (i.e. http://www.example.com/somealias/) the js and css paths are wrong (it should be '/somealias/js/somejs.js' or '/somealias/css/somecss.css').

I'm still very new to symfony project and FrontController, RequestMapper patterns. So I dont know if symfony can deal with the alias setup (path_info maybe different and affect request mapping?!?). I'm still waiting for the answer.

Regards,
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by fabien » Wed Oct 26, 2005 7:17 am

xiblack,

symfony is not yet fully compatible with Alias settings.

Since your first post, I'm working on it and I think I will commit a new symfony version later this week with full alias support and documentation on how to do it.

Since then, if you want to start playing with it, here are the changes i made to make it work:

1 - change in web/.htaccess, line 16-17:

Code: Select all
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]


to

Code: Select all
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ /youraliasname/index.php [QSA,L]


2 - add in youappname/config/settings.yml under the 'all/.settings' category a relative_url_root:

Code: Select all
all:
  .settings:
     relative_url_root:      /youraliasname


3 - change the _compute_public_path() function in lib/symfony/helper/AssetHelper.php to add the relative url root support:

Code: Select all
return $source;


to

Code: Select all
return SF_RELATIVE_URL_ROOT.$source;


4 - change lib/symfony/request/sfRequest.class.php (line 65):

Code: Select all
     // simulate PATH_INFO if needed
     if (!isset($pathArray[SF_PATH_INFO_KEY]))
     {
      $pathInfo = preg_replace('/^'.preg_quote($pathArray['SCRIPT_NAME'], '/').'/', '', $pathArray['REQUEST_URI']);
       $pathInfo = preg_replace('/'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo);
     }


to

Code: Select all
     // simulate PATH_INFO if needed
     if (!isset($pathArray[SF_PATH_INFO_KEY]))
     {
      $script_name = $pathArray['SCRIPT_NAME'];
      $pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathArray['REQUEST_URI']);
      $prefix_name = preg_replace('#\/[^/]+$#', '', $script_name);
      $pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathArray['REQUEST_URI']);
       $pathInfo = preg_replace('/'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo);
     }


OK, that's it. Now, you must have full Alias support.

Give it a try and tell me is all is OK. It so, I will commit these changes and write a tutorial tomorrow.

Fabien
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by xiblack » Wed Oct 26, 2005 9:35 am

Thank you very much for your very clear and detailed response.
I will try it and let you know the outcome.

Regards,
xiblack
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by xiblack » Wed Oct 26, 2005 10:52 am

First, note that I use the last version checkout from your SVN repository (0.5.15).
I set the application name to 'myapp', the default module to 'default1' and default action to 'index1'

Here are my feedbacks:

* Step 4 - Should it be 'lib/symfony/request/sfWebRequest.class.php' (line 278)?!?
I could not find the lines you mentioned in sfRequest.class.php (line 65) so I tried in sfWebRequest.class.php (line 278). And the outcomes:

* In production mode:
The final outcome is OK - sfRouting is now working correctly and I can access the default page using
http://localhost/myalias/
or http://localhost/myalias/default1/
or http://localhost/myalias/default1/index1/

* In debug mode:
There are somethings strange here, I dont know if they are errors/defects or not:
http://localhost/myalias/myapp_dev.php/default1/
and http://localhost/myalias/myapp_dev.php/default1/index1/ ---> go correctly to the default page I want

http://localhost/myalias/myapp_dev.php --> go to Error page (I thought it should be the default page?!?)

* Also in debug mode:
The Debug panel is still using the absolute path to javascript and image files (/sf/js/...)
I found the class responsible for this is lib/symfony/debug/sfWebDebug.class.php, a lot of path are hard-coded inside. For the solution, I dont know which is better: use the AssetHelper functions here to modify the path or change "/sf/js/..." to "'.SF_RELATIVE_URL_ROOT.'/js/...'


Thanks fabien, I now can code my first symfony project on my favourite Alias setup,

xiblack
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by xiblack » Wed Oct 26, 2005 11:37 am

One more thing I just find out with the new hacking above: link helpers are not support Alias in Production mode. In Debug mode, they are OK.
After some searching, I found the link_to and url_for helpers are quite complicated. I dont have any proposal now, maybe after I dive into the code again.

Regards,
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by fabien » Wed Oct 26, 2005 8:40 pm

xiblack wrote:

* Step 4 - Should it be 'lib/symfony/request/sfWebRequest.class.php' (line 278)?!?
I could not find the lines you mentioned in sfRequest.class.php (line 65) so I tried in sfWebRequest.class.php (line 278). And the outcomes:


You're right, it is in sfWebRequest.class.php

xiblack wrote:
* In debug mode:
There are somethings strange here, I dont know if they are errors/defects or not:
http://localhost/myalias/myapp_dev.php/default1/
and http://localhost/myalias/myapp_dev.php/default1/index1/ ---> go correctly to the default page I want


That's ok. The default action is optional in the default routing rule set:

Code: Select all
default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/*


xiblack wrote:
http://localhost/myalias/myapp_dev.php --> go to Error page (I thought it should be the default page?!?)


The default page URL is
http://localhost/myalias/myapp_dev.php/.
Notice the /.

xiblack wrote:
* Also in debug mode:
The Debug panel is still using the absolute path to javascript and image files (/sf/js/...)
I found the class responsible for this is lib/symfony/debug/sfWebDebug.class.php, a lot of path are hard-coded inside. For the solution, I dont know which is better: use the AssetHelper functions here to modify the path or change "/sf/js/..." to "'.SF_RELATIVE_URL_ROOT.'/js/...'


The sfWebDebug class is still rough around the edges and it must be rewritten to use a more real template and helpers. You can open a ticket on this subject to track this issue.
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by fabien » Wed Oct 26, 2005 8:42 pm

I don't have time tonight to dig into this code to see the problem. You can open a ticket so I remember I have to look at it later this week.

Thanks for testing this new Alias code. Once we fixed the remaining problems, I will release a new beta version with some documentation .
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by xiblack » Thu Oct 27, 2005 3:36 am

fabien wrote:
The default page URL is
http://localhost/myalias/myapp_dev.php/.
Notice the /.


Thanks for the note. It works now.
I have created 2 tickets in Symfony Trac regarding Alias setup and sfWebDebug class. I dont know if I can help you in any way to contribute to symfony project. I'm glad to help.

xiblack
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by fabien » Thu Oct 27, 2005 8:54 am

Alias support is now in svn r16.

The r16 commit contains the patch I described sooner in this thread and fix the #9 ticket (url_to).

Thanks to xiblack for testing things.

xiblack, all participation and contributions are welcome. You can contribute patches, you can test symfony and find bugs as you did or write some documentation. We really want symfony to become a community project rather than a one-person project.
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by cerad » Thu Oct 27, 2005 6:50 pm

Not sure if this is related to your alias work or not.

Under 0.4.1 I can start with something like :
http://localhost/symfony/ProjectOne/index.php
And get the congrats page.

Under 0.5.17 I get a cannot find url page.

Under 0.4.1 the request object has no module name so 'default' is used and all is well.

Under 0.5.17 the modules name is returned as /index.php

I don't understand all the routing parsing stuff yet. But I'm hoping that this is something being worked on. If not I'll dig deeper.
cerad
Junior Member
 
Posts: 11
Joined: Sun Oct 23, 2005 7:02 pm

Re: Access symfony project using Apache Alias

by cerad » Thu Oct 27, 2005 9:11 pm

Just a quick followup:
http://localhost/symfony/ProjectTwo/index.php/default
http://localhost/symfony/ProjectTwo/index.php/ModTwo

Both seem to work fine with ModTwo being a new module.

Have not tried forms or redirecting yet.
cerad
Junior Member
 
Posts: 11
Joined: Sun Oct 23, 2005 7:02 pm

Re: Access symfony project using Apache Alias

by fabien » Thu Oct 27, 2005 9:46 pm

I will try to take a look at this problem tomorrow.
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by cerad » Thu Oct 27, 2005 9:54 pm

Thanks. By the way all the scaffoding code generated fine and it works. The onlly strange thing is that I'm getting links like:
http://localhost/symfony/ProjectTwo/index.php/post/edit/id/1 .html

Not sure where the .html is coming from. I can remove it and things still work. It's also inside of the form's action method.
cerad
Junior Member
 
Posts: 11
Joined: Sun Oct 23, 2005 7:02 pm

Re: Access symfony project using Apache Alias

by fabien » Fri Oct 28, 2005 3:47 pm

the .html is a suffix you can modify in the settings.yml (suffix). It's not mandatory but it allows you to add a suffix to all your pages (for search engines for example).

I think we added a small paragraph about the suffix parameter in the book today.
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am

Re: Access symfony project using Apache Alias

by cerad » Fri Oct 28, 2005 4:05 pm

I saw that but it doesn't seem to accept a blank. If I do:
all:
.settings:
suffix: .HTML

Then I get stuff with .HTML appended which indicates I have the right file. But when I blank it out I still get the default of .html.

It's not a big deal but things like:
http://localhost/symfony/ProjectTwo/index.php/comment/show/i d/1.html
Just don't seem right.

By the way:
http://localhost/symfony/ProjectTwo/index.php/
works as long as you have that trailing /
Just need a tweak in there somewhere to reconize that index.php is the main script and not the module.

All under 0.5.17
cerad
Junior Member
 
Posts: 11
Joined: Sun Oct 23, 2005 7:02 pm

Re: Access symfony project using Apache Alias

by cerad » Fri Oct 28, 2005 4:50 pm

Looks like I was being overly optimistic about things.

Things worked well using the 'dev' enviroment but when I switched to 'prod' the urls change:

dev http://localhost/symfony/ProjectTwo/index.php/post/list.html
prod http://localhost/post/list.html
cerad
Junior Member
 
Posts: 11
Joined: Sun Oct 23, 2005 7:02 pm

Re: Access symfony project using Apache Alias

by xiblack » Mon Nov 07, 2005 6:09 am


Things worked well using the 'dev' enviroment but when I switched to 'prod' the urls change:

dev http://localhost/symfony/ProjectTwo/index.php/post/list.html
prod http://localhost/post/list.html

I faced the same problems before, but with the latest svn code and following the article properly: http://www.symfony-project.com/content/book/page/web_server. html, all the problems are solved.
Maybe you missed somethings in the configuration.



I saw that but it doesn't seem to accept a blank. If I do:
all:
.settings:
suffix: .HTML

Then I get stuff with .HTML appended which indicates I have the right file. But when I blank it out I still get the default of .html.

It's not a big deal but things like:
http://localhost/symfony/ProjectTwo/index.php/comment/show/i d/1.html
Just don't seem right.

You should configure the settings to:
Code: Select all
all:
  .settings:
     suffix:  ""   
;or  suffix:  ''


If you just leave the suffix to blank:
Code: Select all
all:
  .settings:
     suffix:

sfDefineEnvironmentConfigHandler class will override 'suffix' with the default setting which is '.html' (defined in SF_SYMFONY_DATA_DIR.'/symfony/config/settings.yml'

One more note: if you change the suffix to something other than .html and "", I think you should also modify the .htaccess file in the web folder (change 'html' to the suffix you choose). I dont know if it is correct, but I had to do that before my own suffix work.

Can anyone confirm on this?

Thanks,

xiblack
xiblack
Member
 
Posts: 34
Joined: Tue Oct 25, 2005 4:59 am

Re: Access symfony project using Apache Alias

by fabien » Mon Nov 07, 2005 8:44 am

I confirm you're right. If you change the suffix, you also have to change the .htaccess.
fabien
Faithful Member
 
Posts: 487
Joined: Sat Oct 22, 2005 9:01 am