以'.php'结尾的Laravel路线不起作用

Using Laravel, I want to create a route to replace functionality of a legacy URL that used to be a PHP file, but whenever I create a route that ends with .php it does not recognize the route and I get a 404.

Route::get('test.php', function(){return "test";}); // Gets a 404 error.

However other routes with extensions seem to work.

Route::get('test.txt', function(){return "test";}); // works fine

see mod_rewrite as its a part of apache mod_rewrite handling this, not the laravel framework alone. see into public/.htaccess file .

I don't see any reason to do this. But as you want to know i am telling. I don't recommend this as its avoiding some purposes of the framework.

At first what you need to do is edit your .htaccess file rewrite rules to enable php extension. i can't say much as i don't see those files here.

Second thing is that all requests will be by default redirected to index.php . see one of my laravel project public/ folder .htaccess file. you need to look into yours and modify that, there is lot of ways you can modify according to your needs. there could be other .htaccess files

for extension changing part look here

RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] part for changing index.php

my .htaccess file as it is not modified

<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]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

In laravel your routes do not need a .php extension.

check out the documentation.

Also when you return views you do not need the .php extension.