How Symfony routing works - php

I am a beginner for Symfony and following the Symfony documentation and it has a create page tutorial
and after creating this this when I test this using the browser,
I can access it using the following URLs
localhost:8000/lucky/number and
localhost:8000/app_dev.php/lucky/number
but when I try to access it using the usual way like
localhost/appname/lucky/number
it gives 404 and I use Ubuntu with Apache2 localhost so I need to know the idea behind this Symfony3 routing and why I cannot access the page using usual localhost/cmstest/lucky/number
Thanks

Following the Symfony doc, this is how your vhost should looks like:
<VirtualHost *:80>
DocumentRoot /var/www/MySymfonyProject/web
<Directory /var/www/MySymfonyProject/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory /var/www/MySymfonyProject/web/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
ErrorLog /var/log/apache2/MySymfonyProject_error.log
CustomLog /var/log/apache2/MySymfonyProject_access.log combined
</VirtualHost>
Now that you have a clean vhost, here are some details/instructions:
All terminal commands are done as admin (sudo).
In terminal, run this command a2enmod rewrite
In terminal, as you don't use any URL, make sure to disable the default vhost a2dissite 000-default.conf
In terminal, make sure to enable your site vhost a2ensite myVhost.conf
Make sure your project directory is right /var/www/MySymfonyProject
In terminal, restart apache service with service apache2 restart
Access your site through localhost or localhost/app_dev.php (you don't have to specify the bundle name)

Because you have to use the web/ directory (which is the public root of your application, only this directory should be accessible using the browser).
When using server:run, it'll use web/ as your server root automatically. When accessing via the directory structure, include it in the URL: http://localhost/appname/web/lucky/number

Hewwo PM-Riabel~
Can you paste here your virtual-host config?
Your first problem may be in it...
Do you have only one virtual-host active on your machine?
I would guess you try to view it from Linux (correct me if it's not the case)
You should set a fictive URL to your hosts file (myurl.com) and redirect it to 127.0.0.1 (localhost)
Then you can configure your virtual-host to work with this same URL. (Better do it when you want to work multiple projects)
Cordially,
Preciel.

Related

Configuring Apache for Symfony

I'm using Symfony 3.4 and Apache 2.4 (on Ubuntu); and I'm trying to get my Symfony project running through Apache instead of the built-in server. I edited the default .conf file in the "sites-available" folder to the following:
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/my_project/web
<Directory /var/www/html/my_project/web>
AllowOverride All
Require all granted
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
When trying any route, all i get is a "requested URL not found" error. Simply going to "localhost"takes me to app.php, which obviously does nothing. I've always restarted apache after every edit. What am i missing (besides a brain)?
If you use composer (which is default for Symfony projects), you can just do composer require symfony/apache-pack, which should install default working .htaccess file into your public directory.

Apache2 Vhost doesn't works with Symfony 2.8

I started symfony 2.8 project, and i have a problem with Apache2 vhosts.
When i try to get my admin home page, i have a 404 error.
that is my project vhost file (with 777 rights for tests) in /etc/apache2/sites-enabled directory:
<VirtualHost *:80>
ServerName local.erdf.fr
ServerAdmin webmaster#localhost
DocumentRoot /var/www/erdf/web
<Directory /var/www/erdf/web/>
DirectoryIndex app_dev.php
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
</IfModule>
</Directory>
ErrorLog /var/log/apache2/errors.log
</VirtualHost>
(i restarted apache2 service after file creation)
I also thought that the problem could possibly come to misconfiguration in my routing config.
So that is my app/config/routing.yml:
admin:
resource: "#AdminBundle/Resources/config/routing.yml"
prefix: /admin
And my AdminBundle/Resources/routing.yml config:
admin_homepage:
path: /
defaults: { _controller: AdminBundle:Default:index }
(i also tested with "pattern" argument instead of path).
In my local computer, i added in my /Etc/hosts:
192.168.1.20 local.erdf.fr
So, whend i try to access to url: local.erdf.fr/web/app_dev.php/admin, i have a 404 error.
(and if i just try local.erdf.fr/web/app_dev.php/ i have the apache2 default page in /var/www/html).
I hope I have been clear enough and thank you in advance for your help.
PS: I just created this server. Perhaps should I change a file in apache2 config to consider the vhosts ?
When i just use the url: local.erdf.fr, i have the /var/www/ racine:
EDIT: SOLVED
Ok so, thx to #Rufinus who put me on the road. indeed when i installed my apache server, i already added the
sudo a2enmod rewrite
But i forget to add at the end of /etc/apache2/apache2.conf this:
<ifModule mod_rewrite.c>
RewriteEngine On
</ifModule>
Thank you all for your precious help :)
(and sry for my bad english)
Try to access by local.erdf.fr/app_dev.php/ since your VHOST already aim to /web/ ?
Edit :
Does the ip is right ?
Try to remove the trailing / in
<Directory /var/www/erdf/web/>
And remove the
DirectoryIndex app_dev.php

Configuration of laravel under apache to avoid public

I recently installed laravel under a LAMP server, but can't get it to work if I don't specify public in the url for another routes than the index.php.
This is my apache site config (/etc/apache2/sites-available/furbook.com.conf):
<VirtualHost *:80>
ServerName furbook.calhost.com
DocumentRoot "/var/www/html/furbook.com/public"
<Directory "/var/www/html/furbook.com/public">
AllowOverride all
</Directory>
</VirtualHost>
Then created a symbolic link from /etc/apache2/sites-enabled:
sudo ln -s ../sites-available/furbook.com.conf
Finally, this is my .htaccess file in furbook/public:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
With this now I'm able to skip the public folder, localhost/furbook.com works redirecting to the public/index.php as expected.
But the problem is when I try to route an url like localhost/furbook.com/cats, then it throws a page not found error.
If I use localhost/furbook.com/public/catsthen works as expected.
What am I missing?
There's not quite enough information to answer you question 100% authoritatively -- here's my best guess.
If I use localhost/furbook.com/public/catsthen works as expected
Apache allows you to setup multiple "VirtualHost" configurations on a single server. A "VirtualHost" is a domain name like furbook.calhost.com that points to a specific folder on your computer. It's called Virtual because, at the time Apache was created, there was a strong bias towards "one host name/one physical server"
<VirtualHost *:80>
ServerName furbook.calhost.com
DocumentRoot "/var/www/html/furbook.com/public"
<Directory "/var/www/html/furbook.com/public">
AllowOverride all
</Directory>
</VirtualHost>
You have setup a virtual host for the name furbook.calhost.com
ServerName furbook.calhost.com
However, in your URL, you're still accessing the system via the name localhost. Accessing via localhost means apache will use the default host/site configuration, which (appears to) still be pointing at the folder above root.
Your options
Setup local DNS or a host file to have furbook.calhost.com point to 127.0.0.1
Change the default configuration of your apache system (probably in httpd.conf) to point to the public folder
Good luck!
(Also, don't forget Apache systems typically require a reset to pickup new configuration changes)

How to remove "web/app_dev.php" from Symfony2 URLs?

I just created my first Symfony2 project. But the "/web/app_dev.php" part in the URL annoys me. It should be possible to do this without Virtual hosts...
But when I try this through .htaccess I always get routing errors and the "web/" is always added to the url...
EDIT: The whole project is also in a subdirectory named "symfonyTest". The Url to the demo page is "http://localhost/symfonyTest/web/app_dev.php/demo/" and it should become "http://localhost/symfonyTest/demo/". Links should also have this syntax. Is this possible?
Symfony2 comes with a built in debug mode, which is what you are using when you access url's with the app_dev.php addition. The debug mode caches significantly less than the production mode which can be accessed by directing your browser to the url, but leaving out the app_dev.php. If accessing this url doesn't work then it probably means that you need to clear your production cache.
To clear the cache direct you terminal (command console) to the root of you Symfony project. From there type the following command:
php app/console cache:clear --env=prod --no-debug
Per comments below in Symfony 3 this has moved:
php bin/console cache:clear --env=prod --no-debug
This should clear your productions cache and allow you to access urls without the app_dev.php.
As for the web part of the url. The easiest way to remove that is to set the web root of your project to the web folder. It's best to do this with apache using virtual hosts, but there are ways to do it with the htaccess.
This link explains how to use the htaccess file to change the webroot.
http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/
Hope this helps!
<VirtualHost *:80>
DocumentRoot "path_to_symfonyTest/web"
ServerName "symfonyTest"
<Directory "path_to_symfonyTest/web">
DirectoryIndex app_dev.php
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
RedirectMatch permanent ^/app_dev\.php/(.*) /$1
</Directory>
</VirtualHost>
That's part of my httpd.conf
I used to have this problem too, please take a look at my configurations and try them out
inside my .htaccess:
Hi all, I have been having this problem too. And it seems that the "stock" .htaccess is the problem. I have solved this in my environment and here is my .htaccess with notes:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app_dev.php [L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the startpage to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
inside my "symfony" entry in /etc/apache2/sites-available:
<VirtualHost 127.0.1.15>
ServerAdmin webmaster#localhost
ServerName symfony
DocumentRoot /var/www/Symfony/web
DirectoryIndex app.php
<Directory /var/www/Symfony/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
If you use an apache virtual host, you can get it working the way you desire. Here is an example virtual host from my xampp:
<VirtualHost *:80>
ServerName myurl.local
# Basic stuff
DocumentRoot "C:/path/to/symfony/web"
DirectoryIndex app.php
<Directory "C:/path/to/symfony/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
After restarting your apache, you can access the project through http://myurl.local (don't forget to configure your hosts file under C:\Windows\system32\drivers\etc\hosts!). If you want to have the dev environment (app_dev.php) not showing up in the url, change the DirectoryIndex to app_dev.php.
//enable mod rewrite
sudo a2enmod rewrite
Change all occurrences of AllowOverride None by AllowOverride All File: (/etc/apache2/apache2.conf) or (/etc/apache2/sites-available/000-default.conf) - for me work with /etc/apache2/apache2.conf file.
Example:
<Directory /var/www/web/>
# enable the .htaccess rewrites
Options Indexes FollowSymLinks
#AllowOverride None #commented
AllowOverride All
Require all granted
</Directory>
//Restart apache service: sudo service apache2 restart
You would need .htaccess file in parent folder of web, however, I wouldn't suggest doing so. That folder, apart from web, contains complete source, binaries, vendors and much more therefore you would need to apply many rules just to allow web-access to web folder and disallow web-access to everything else.
So, it's doable, but I would definitely go for vhost solution...
you just need to copy the content of .htaccess file to the your-website.conf
inside the TAG Directory

Wildcard Subdomain Exceptions

I have a wildcard subdomain enabled and dynamically parse the URL by passing it as-is to my index.php (ex. somecity.domain.com).
Now, I wish to create a few subdomains that are static where I can install different application and not co-mingle with my current one (ex. blog.domain.com).
My .htaccess currently reads:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Can I manipulate this .htaccess to achieve what I need? Can it be done through Apache?
Your .htaccess does nothing useful, as Apache is probably configured with DirectoryIndex index.php. Well, it does move domain.com/a to domain.com/index.php, but I doubt that is what you want.
Your wildcard virtualhost works because you probably have ServerAlias *.domain.com in your configuration, or a single virtualhost and DNS pointing to the address of your server. (When you have a single virtualhost, it shows up for any request, and the first listed virtualhost is the default one)
You have to create new VirtualHosts for the static domains, leaving the default one as, well, the default one :)
Check these tutorials that explain it all.
You'll have to configure apache for those static sub-domains. The "catch-all" site will be the default site configured, so that one will catch the other ones.
I'm not sure I understand completely what you need to accomplish, but it might helpful to setup virtual domains within your Apache configuration file. You can map them to folders on the drive with different applications installed. Each virtual domain is treated much like a root directory. I have my development environment setup locally on my Windows machine a lot like this:
NameVirtualHost *:80
# Begin virtual host directives.
<VirtualHost *:80>
# myblog.com virtual host.
ServerAdmin webmaster#myblog.com
DocumentRoot "c:/apache_www/myblog.com/www"
ServerName myblog.com
ServerAlias *.myblog.com
ErrorLog "c:/apache_www/myblog.com/logs/log"
ScriptAlias /cgi-bin/ "c:/apache_www/myblog.com/cgi-bin/"
<Directory "c:/apache_www/myblog.com/www">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
If this does not help get you on the right track, then try researching the VirtualHost directive to come up with a solution. I find trying to do all this in an .htaccess to be cumbersome and difficult to manage.
I don't know if you have cPanel installed on your host, but I was able to do this by adding a new subdomain * and then sending all that traffic to a particular subdomain, for example: *.domain.com -> master.domain.com. Then you can read out which URL you are at in master.domain.com and go from there.

Categories