Laravel 8.53 - Routing doesnt work on any of pages - php

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>

Related

Why Apache is authenticating but not serving AUTHENTICATE variables in some routes?

I have a Virtual Host in a WAMP server. To acces that VH, you need to authenticate to a LDAP server, so I configured it like this:
<VirtualHost *:80>
ServerName myserver
DocumentRoot /apps/portal/public
<Directory "/apps">
Options FollowSymLinks
AllowOverride None
AuthType Basic
AuthBasicProvider ldap
AuthName "My Autenticacion"
AuthLDAPURL "ldaps://myldapserver:636/DC=DOM1,DC=COM?sAMAccountName,memberOf?sub?(objectClass=*)"
AuthLDAPBindDN myuser
AuthLDAPBindPassword mysecret
LDAPReferrals Off
Require valid-user
</Directory>
# My Laravel 7 app:
Alias /myapp /apps/myapp/public
<Directory "/apps/myapp/public">
# Laravel rewrites:
RewriteEngine On
RewriteBase /myapp/
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
</VirtualHost>
So, all applications are under the /apps local directory. You can access the host via http://myserver. You can access the Laravel 7 application via http://myserver/myapp.
In that application I read the contents of the PHP variable $_SERVER['AUTHENTICATE_MEMBEROF']. When I visit http://myserver/myapp everything goes fine. But let's say I define one route like this:
Route::get('about', function() {
return view('about');
})->name('about');
Really simple. But as soon as I go to http://myserver/myapp/about, I get this error:
Undefined index: AUTHENTICATE_MEMBEROF
Furthermore, I've realized that none of the AUTHENTICATE_* server variables are served in this page. However, if I want to access http://myserver/myapp/about, the server prompts for credentials. So it's authenticating the user, but not serving the AUTHENTICATE_* variables.
What am I missing?
EDIT: Well, finally, I've found out that in all these routes, all AUTHENTICATE_* variables are available through a different name, which is REDIRECT_AUTHENTICATE_*, which doesn't seem to be documented anywhere. I assume this has to do with the rewrites, but anyone could explain why this new names, and whether it is possible or not to avoid these renamings?

PHP and Redirect with apache2.conf

I have an internal domain like
live.domain.com
which I defined in sites-available like so
ServerName live.domain.com
ServerAdmin webmaster#localhost
DocumentRoot /data/www/html/mydomainnamenodots
<Directory /data/www/html/mydomainnamenodots>
Require all granted
</Directory>
and the accordingly same for SSL conf.
I do have a folder /api which is in there like
/data/www/html/mydomainnamenodots/api
What I want is to catch all calls to
live.domain.com/api/*
and redirect them to something like
live.domain.com/api/index.php?url=*
So a real example would be
live.domain.com/api/statistics/getFTE/123/456
and I'd like that to be
live.domain.com/api/index.php?url=/statistics/getFTE/123/456
That would result in a $_REQUEST in index.php like so
array(1) {["url"]=>string(33) "/statistics/getFTE/123/456"}
mod_rewrite is enabled and loaded. Simple tests with file to file redirects work.
My rewrite rule which does not work as it does not redirect is
<Directory "/data/www/html/mydomainnamenodots/api">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*?mydomainnamenodots\/api(.*)$ /mydomainnamenodots/api/index.php?url=$1 [QSA,L]
</Directory>
Any ideas why that does not work?
Am I missing the RewriteBase or something?
Thanks in advance
Alex
As the subdomain already points to a subfolder, it has to be the the according subfolder below that one in the directory definition. In addition I changed the regexp as well.
Here is what worked for me:
<Directory />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*\/api(\/.*)$ /api/index.php?url=\/$1 [QSA,L]
</Directory>

How to get access of other pages inside of index.php by PHP?

I installed PHP and Apache server in my computer.
So inside of "htdocs" I created 2 files(index.php, Contact.php) and a directory(MyClass), after that inside of "MyClass" I created a file(class.php)..
In web browser when I am using the url "http://localhost/MyClass/class.php", the result is : "class.php" sending data to the web browser.
In the same situation is there any way in PHP/Apache to take control of it from the "index.php" ??
Or
I want to be known about all the requests inside of "index.php" which came from web browser, is it possible ????
But I don't want to use any GET variable like "http://localhost/index.php?class=page2"..
Apology for my bad English.
Thanks..
You should use include, in your case you would use
include 'MyClass/class.php';
More information about include can be found right here
I'm not sure I understand correctly but a way to not use ?class=page2
is to create a .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will rewrite all requests to non existing files or folders to your index.php
the use $_SERVER['REQUEST_URI'] to make your navigation.
for example you could use http://localhost/class/page/2
$_SERVER['REQUEST_URI'] would then be class/page/2
If your website is in a subfolder of htdocs be sure to edit
RewriteBase /dir/here/
[...]
RewriteRule . /dir/here/index.php [L]
to match it
My problem solved.
Which changes I did they are below :
In "C:\Apache24\conf" need to change file "httpd.conf"
Just active :
1)
LoadModule rewrite_module modules/mod_rewrite.so
2)
<Directory />
#AllowOverride none
AllowOverride All
Require all denied
</Directory>
3) I am using "Virtual Host", so in "C:\Apache24\conf\extra" need to change file "httpd-vhosts.conf"
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#test.com
DocumentRoot "E:/TEST"
<Directory "E:/TEST">
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/test.com-error.log"
CustomLog "logs/test.com-access.log" common
</VirtualHost>
If you are not using "Virtual Host", then I think you need to add some lines to the "Directory" inside of "httpd.conf" !!
<Directory>
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
Use the PHP include function. You can include your MyClass/class.php in you index file. You can then add an htaccess file to restrict files in the MyClass directory from being viewed directly.

Laravel routes do not work at all

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/

Symfony 2 project in nested directory

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!

Categories