My laravel routes are not working at all. I tried something like this:
Route::get('welcome', function () {
return View::make('welcome');
});
Accessing it with localhost/project/project/public/welcome works fine. I have tried it in many ways but seems like routes aren't working since localhost/project/welcome show me 404 error. I know there is simillar topic but there is no answer for me. Could somebody help me out please?
My htaccess file looks like this (I have never touched it):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You need to edit your HTTP server to have the document root as project/project/public/
For example in Apache you can do something like that:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/project/project/public/
And in Nginx it will like that:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/project/project/public/;
You have said that you can access your laravel app throught
localhost/project/project/public/welcome. This is because this path is your starting root path, from where you start your application.
Example that will allow you to access new route
Route::get('other_route', function () {
return View::make('welcome');;
});
This code can be accessed, if you will type localhost/project/project/public/other_route into your browser
localhost/project/welcome won't work because your application is deeper than this path.
You should setup virutal host for your application so that your path could be myapp.local/welcome
myapp.local/other_route
Or access your application, assuming that your start point is http://localhost/project/project/public/
Related
Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine)
laravel1.test
How to add Virtual host:
Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>
I can't pass the problem with routing in Laravel 8.5 framework. I know there have been many requests about this problem so far but any of given solutions didnt help in my case. I will show Frontend code, since backend is the same thing and also doesnt work. Right now, the only thing that works is index.php.
I know thanks to newest Laravel all bugs are hidden under simple error 404.
Bug image
I already tried:
Changing in apache/conf/httpd solution AllowOverride All
Clearing whole cache and routes cache
Any possible misspells in code
Still, without any working anwser.
My FrontendController at app/Http/Controllers
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FrontendController extends Controller
{
public function index()
{
return view('frontend.index');
}
public function object()
{
return view('frontend.object');
}
public function article()
{
return view('frontend.article');
}
public function person()
{
return view('frontend.person');
}
public function room()
{
return view('frontend.room');
}
public function roomSearch()
{
return view('frontend.roomsearch');
}
}
My web.php at app/routes
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontendController;
##Frontend routes
Route::get('/','FrontendController#index')->name('index');
Route::get('/object','FrontendController#object')->name('object');
Route::get('/article','FrontendController#article')->name('article');
Route::get('/person','FrontendController#person')->name('person');
Route::get('/room','FrontendController#room')->name('room');
Route::get('/roomSearch','FrontendController#roomsearch')->name('roomSearch');
##Backend routes
Route::group(['prefix'=>'admin'],function(){
Route::get('/','BackendController#index')->name('adminHome');
Route::get('/cities','BackendController#cities')->name('cities');
Route::get('/myObjects','BackendController#myobjects')->name('myObjects');
Route::get('/profile','BackendController#profile')->name('profile');
Route::get('/saveObject','BackendController#saveobject')->name('saveObject');
Route::get('/saveRoom','BackendController#saveroom')->name('saveRoom');
});
.htacces file in app/public
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymlinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My routes
image
Solution
The solution was found thanks to #Gert B.
Simply add to your Laravel application any virtual host (I added for mine) laravel1.test
How to add Virtual host: Go to C:\Windows\System32\drivers\etc\hosts
add line:
127.0.0.1 laravel1.test (or your virtual host name)
And add this to your vhosts(in case of using xampp) in C:\xampp\apache\conf\extra httpd-vhosts
<VirtualHost *:80>
ServerName www.laravel1.test
ServerAlias laravel1.test
DocumentRoot "C:\xampp\htdocs\Laravel1\public"
<Directory "C:\xampp\htdocs\Laravel1\public">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php
# ...other settings...
# Apache 2.4
Require all granted
## Apache 2.2
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>
Version Information: Apache v2.4.18 on Ubuntu, PHP version 7.1,Symfony v3.2
I've been trying to get this working for a couple of days now and keep hitting problems, I have an existing PHP application using a custom built framework with the following Apache VirtualHost configuration:
<VirtualHost *:80>
ServerName dvlp.mydomain.com
DocumentRoot /var/www/my-site/com
RewriteEngine On
# Do nothing for the Home page ('^/$'), specific directories, static files.
RewriteRule ^/(?:$|shared|asset)|\.(?:php|ico|txt|xml) - [L]
# Search engine friendly request URIs (most, but not all, without a query string).
RewriteRule ^/([a-z0-9-]+/?)$ /index.php?param1=$1 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$ /index.php?param1=$1¶m2=$2 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1¶m2=$2¶m3=$3 [QSA]
<Directory />
Options FollowSymLinks
AllowOverride None
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
What I am trying to do is install a Symfony application to work within a sub-directory. I've created my Symfony application in /var/www/symfony-app and created a symbolic link /var/www/my-site/com/my-symfony which points to /var/www/symfony-app/web.
I then tried the following on line 6 of my VirtualHost file:
RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]
Visiting dvlp.mydomain.com/my-symfony in the browser takes me to the directory listings for the /var/www/symfony-app/web folder. I tried adding an index.php file to /var/www/symfony-app/web which simply includes app_dev.php and it loaded the home page, but I I tried visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the 404 error page from my existing application.
I realised now that I need separate rewrite rules for this sub-directory, for Symfony this is usually written as the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
So I undid the change I made to line 6 of the VirtualHost file and added a new line as follows:
RewriteRule ^/my-symfony(.*)$ /my-symfony/index.php [L,QSA]
This results in a 404 error:
No route found for "GET /my-symfony/" (from "http://dvlp.mydomain.com/")
And if I try visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the same 404 error:
No route found for "GET /my-symfony/edit-personal-details" (from "http://dvlp.mydomain.com/")
The last thing I have tried is adding a prefix to my Symfony routing.yml:
my_symfony:
resource: "#MySymfonyBundle/Controller/"
type: annotation
prefix: /my-symfony
This then loads the pages correctly but with no images or stylesheets because they are trying to load from the DocumentRoot (e.g dvlp.mydomain.com/css instead of dvlp.mydomain.com/my-symfony/css).
I would appreciate any input as to where I am going wrong. Thanks.
I finally managed to get this working as intended by adding a "Location" tag containing specific RewriteRules and a RewriteBase for the Symbolic link. For information, final VirtualHost config below:
<VirtualHost *:80>
ServerName dvlp.my-domain.com
DocumentRoot /var/www/my-site/com
RewriteEngine On
<Location /my-symfony>
RewriteBase /my-symfony
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</Location>
# Do nothing for the Home page ('^/$'), specific directories, static files.
RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]
# Search engine friendly request URIs (most, but not all, without a query string).
RewriteRule ^/([a-z0-9-]+/?)$ /index.php?param1=$1 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$ /index.php?param1=$1¶m2=$2 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1¶m2=$2¶m3=$3 [QSA]
<Directory />
Options FollowSymLinks
AllowOverride None
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
I have the following httpd structure:
c:/
xampp/
htdocs/
mysite/
bower_components/
app/
index.html
app.js
views/
api/
index.php
.htaccess
Inside the app folder I have my application code: HTMLs, JSs, CSSs, etc. In other words my frontend.
Inside the api folder I have my PHP REST api: index.php. In other words my backend.
As said I am using AngularJS and would like to enable html5Mode, so I can have a cleaner URL.
I manage to put just the frontend to work with the following configuration in Apache:
NameVirtualHost *:8081
<VirtualHost *:8081>
ServerName localhost:8081
DocumentRoot "C:/xampp/htdocs/mysite/app"
Alias "/bower_components" "C:/xampp/htdocs/mysite/bower_components"
<Directory "C:/xampp/htdocs/mysite/app">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
With the configuration above I am able to run my frontend like a charm.
http://localhost:8081 takes me to index.html and all my states in my $stateProvider are also resolved.
But my API calls fail and I don't know what to do.
I have the following REST service in my index.php:
require 'vendor/autoload.php';
// Create Router instance
$router = new \Bramus\Router\Router();
$router->get('/cart/items', function() {
echo '{"items":[ {"name":"item1"}, {"name":"item2"} ]}';
});
// Run the Router
$router->run();
When I call from my frontend:
$http({
url: '/api/cart/items',
method: 'GET'
}).then(function (response) {
// do stuff...
}, function (error) {
alert(error.data);
});
The call is falling in the rewrite rule to index.html. All my API calls are responding the index.html.
In my api folder I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Options -Indexes
I have already tried to put an Alias in the Apache configuration for the api folder but the result was the same.
I've even tried to point my DocumentRoot to C:/xampp/htdocs/mysite. That way my calls to the API are resolved but when I access http://localhost:8081 I get the list of files and folders from mysite.
It seems so close and yet so far. Hope you guys can enlight me.
I have domain as example.dev and i want to redirect it to www.example.dev in laravel 5. In local environment i have setup virtual host as example.dev and also setup it's alias as www.example.dev.
my .htaccess file on laravel 5 looks like this.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
RewriteBase "/"
and my routes file in laravel looks like this.
Route::group(array('domain' => 'example.dev'), function()
{
Route::get('/', function() {
return "I'm root!";
});
});
Route::group(array('domain' => '{sub}.example.dev'), function()
{
Route::get('/', function($sub) {
return "I'm ".$sub." subdomain";
});
});
now if i visit at example.dev, i get output as
I'm root
and if i visit at something.example.dev, i get
I'm something subdomain
and if i visit at www.example.dev, i get this...
I'm www subdomain
what i could think of is to define another custom domain group with www but this is not so good i believe or not good practice. i tried with some modification in my .htaccess file as follow after RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
and now if i visit example.dev it redirect be at www.example.dev but still the output is
I'm www subdomain
then i changes my first routing group from example.dev to www.example.dev and it worked! but i just need to know if this is the right way to do this or not? or using groups for multiple sub-domains is the only way ? and do you know any easy and best way to check subdomains from database through controller so i will check it that subdomain exists then serve with it's view otherwise pass not found error. i know somewhere it can be done with filter and using before but don't know exactly!
help would be really appreciated.
thanks :)
Route::group(array('domain' => '{sub}.example.dev'), function($sub)
{
if($sub == 'www' || $sub == ''){
Route::get('/',function(){
return "I'm root!";
});
}
else{
Route::get('/', function($sub) {
return "I'm ".$sub." subdomain";
});
}
});
I need to deploy a Symfony 2 project in nested directory on production server. Effectively, this would mean that all URLs are prefixed with /subdirectory/ path, i.e.
http://host.com/subdirectory/project/web/app.php/survey
I don't need URL rewriting and are am not going to set it up. The app should work when accessed via above URL only.
The problem I have is that all links generated by path and asset Twig functions are relative to server root (/), and not subdirectory the project is in (/subdirectory/). Is there any config parameter in Symfony 2 to override relative paths globally?
I tried to work around that problem by adding HTML tag, but it doesn't work for links.
Update: I owe you further details. I'm running IIS with its version of mod_rewrite, so part of your suggestions may still be valid.
Update: Ideally I would like to see a solution that sets root dir on AppKernel object - method AppKernel::setRootDir() had it existed to complement existing AppKernel::getRootDir().
Strange... with default config, path/asset should handle subdirectories. Have you tried it with symfony distribution out-of-the-box? On localhost im mostly using it this way.
Would you mind posting your config file?
Anyway...
For assets paths you could try to configure:
#config.yml
templating:
assets_base_urls: { http: "http://yoursite.com/dir1", ssl: "https://yoursite.com/dir1" }
(http://symfony.com/doc/current/reference/configuration/framework.html#assets-base-urls)
For router you should read: http://symfony.com/doc/current/cookbook/console/sending_emails.html#configuring-the-request-context-globally
It could give you some ideas about changing router context like:
# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: http
router.request_context.base_url: my/path
Anyway - context is automaticlly set based on request information. Try to debug:
$context = $this->getContainer()->get('router')->getContext();
// you should be mainly interested in:
$context->getBaseUrl();
It is possible to easily create listener class which will manually set your base url if anyhow it is wrong:
class RouterContextListener {
protected $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelRequest(Event $event)
{
$this->router->setBaseUrl('somedir');
}
}
You are looking for RewriteBase rule, add it to your in your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/project/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
Also take a look onto symfony-standard 2.3 .htaccess file, mb it can help
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
Look at app/config/routing.yml where you import routing from your bundles. Records in that file would look similar to this:
somename:
resource: "#YourBundle/Resources/config/routing.yml"
prefix: /
You can add your subdirectory in prefix, like prefix: /subdirectory
Then all generated URLs will include subdirectory.
To enable it the rewrite module, run "apache2 enable module rewrite":
sudo a2enmod rewrite
You need to restart the webserver to apply the changes:
sudo service apache2 restart
VirtualHost should be like this
ServerName localhost.myproject.dev
ServerAdmin webmaster#localhost
DocumentRoot /var/www/project_path
<Directory /var/www/project_path>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
</Directory>
this option is redirect your index.php, index.html or whatever:
Options Indexes FollowSymLinks MultiViews
it will rewrite your htacces!
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</IfModule>
My solution to this on a local environment was adding the following code in app/config/config.yml
framework:
assets:
base_urls:
- 'http://localhost/symfony_dir/'
No rewrite needed!