Laravel 应用程序未在 xampp 或实时服务器上运行,但在 php artisan serve 上运行良好 [英] Laravel App not running on xampp or live server but works well on php artisan serve
问题描述
我的 laravel 应用程序在 xampp 或实时服务器上不起作用,但在 PHP artisan serve 提供的链接上运行良好.每当我在 xampp 上运行此应用程序时,它都会返回 404 错误.我还有其他 Laravel 应用程序,它们在 xampp 上也能正常工作,但只有这个.我找不到任何解决方案来解释为什么我的 laravel 没有在托管服务器或 xampp 上运行.我已经尝试了在相关问题中找到的所有解决方案,但没有发现任何有用的解决方案.
- 我尝试在更改现有 .htaccess 后运行该应用程序根文件夹中的文件.
- 我尝试在公开更改 .htaccess 文件后运行该应用程序文件夹.
- 我尝试在删除其中一个应用程序后运行该应用程序.
- 我已尝试再次使用安装和更新依赖项作曲家.
我应该怎么做才能让它运行?
当我尝试通过 xampp 运行它时,我看到的是错误,但是当人们通过它运行 Web 应用程序时总是会出现文件夹结构.
现在,我的根文件夹中有一个 htaccess 文件.下面是它的代码:
<IfModule mod_negotiation.c>选项 -MultiViews -Indexes</IfModule>重写引擎开启# 处理授权头RewriteCond %{HTTP:Authorization} .重写规则 .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]# 如果不是文件夹,则重定向尾部斜杠...RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} (.+)/$重写规则 ^ %1 [L,R=301]# 处理前端控制器...RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.php [L]</IfModule>
我的 composer.json 文件的代码:
<代码>{名称":laravel/laravel",类型":项目",描述":Laravel 框架",关键词":[框架",拉拉维尔"],许可证":MIT",要求":{php":^7.2",fideloper/proxy":^4.0",jenssegers/mongodb":^3.6",jeroennoten/laravel-adminlte":^3.6",laravel/框架":^6.0",laravel/tinker":^1.0",laravel/ui":^1.1",yajra/laravel-datatables-oracle":~9.0"},要求开发":{立面/点火":^1.4",fzaninotto/faker":^1.4",嘲笑/嘲笑":^1.0",努诺马杜罗/碰撞":^3.0",phpunit/phpunit":^8.0"},配置":{优化自动加载器":真,首选安装":dist",排序包":true},额外":{laravel":{不要发现":[laravel/黄昏"]}},自动加载":{psr-4":{应用程序":应用程序/"},类图":[数据库/种子",数据库/工厂"]},自动加载开发":{psr-4":{测试":测试/"}},最低稳定性":开发",首选稳定":真,脚本":{后自动加载转储":[Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",@php artisan package:discover --ansi"],post-root-package-install":["@php -r \"file_exists('.env') ||copy('.env.example', '.env');\"";],post-create-project-cmd":[@php artisan key:generate --ansi"]}}
我的 package.json 文件的代码:
<代码>{私人":真的,脚本":{dev":npm 运行开发",development":cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js","watch": "npm run development -- --watch","watch-poll": "npm run watch -- --watch-poll",hot":cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",prod":npm 运行生产",生产":cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"},开发依赖":{axios":^0.21.1",引导程序":^4.0.0",跨环境":^5.1",jquery":^3.2",laravel-mix":^4.0.7",lodash":^4.17.13",popper.js":^1.12",resolve-url-loader":^4.0.0",sass":^1.20.1",sass-loader":7.*",vue":^2.5.17",vue-template-compiler":^2.6.10"}}
我的 server.php 文件的代码:
我在公共文件夹中的 index.php 文件的代码:
make(Illuminate\Contracts\Http\Kernel::class);$response = $kernel->handle($request = Illuminate\Http\Request::capture());$response->send();$kernel->terminate($request, $response);
在 xampp 上运行应用程序时出现错误
Laravel App 当前文件夹结构
我也遇到过这个问题.我做了一些研究,我发现可以在 xampp 上运行 Laravel 的唯一方法如下:
- 在 laravel 设置的父文件夹中,我创建了一个名为laravel"的文件夹.并将所有内容移到其中.
- 我进入 laravel/public 并取出其中的所有文件,并将其放入父目录,从而产生以下文件夹结构:(在我的 htdocs 中)
<块引用>
|-.htaccess|-favicon.ico|-index.php|-robots.txt|-web.config\---laravel
- 进入 index.php 并将其加载文件的代码行更改为从新路径加载文件(例如):
<块引用>
if (file_exists(__DIR__.'/laravel/storage/framework/maintenance.php')){需要 __DIR__.'/laravel/storage/framework/maintenance.php';}
- 启动 apache 并检查本地主机,它应该可以工作.
My laravel app does not work on xampp or a live server but it works fine on the link provided by PHP artisan serve. Whenever I run this app on xampp it returns 404 error. I have other laravel apps also which work fine on xampp but this one. I am unable to find any solution to it that why my laravel is not running on a hosted server or xampp. I have tried all the solutions found in related questions but did not find any of those useful.
- I have tried running the app after changing my existing .htaccess file in the root folder.
- I have tried running the app after changing .htaccess file in public folder.
- I have tried running the app after deleting both and one of them.
- I have tried installing and updating the dependencies again using composer.
What should I do to make it running?
What I see when I try to run it through xampp is the errors, but the folder structure that always occurs when one runs a web app through it.
Right now, I have a single htaccess file which is in my root folder. Below is the code of it:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Code of my composer.json file:
{
"name": "laravel/laravel",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [
"framework",
"laravel"
],
"license": "MIT",
"require": {
"php": "^7.2",
"fideloper/proxy": "^4.0",
"jenssegers/mongodb": "^3.6",
"jeroennoten/laravel-adminlte": "^3.6",
"laravel/framework": "^6.0",
"laravel/tinker": "^1.0",
"laravel/ui": "^1.1",
"yajra/laravel-datatables-oracle": "~9.0"
},
"require-dev": {
"facade/ignition": "^1.4",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
"nunomaduro/collision": "^3.0",
"phpunit/phpunit": "^8.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"laravel": {
"dont-discover": [
"laravel/dusk"
]
}
},
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
}
}
Code of my package.json file:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.21.1",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"resolve-url-loader": "^4.0.0",
"sass": "^1.20.1",
"sass-loader": "7.*",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
}
}
Code of my server.php file:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
Code of my index.php file in public folder:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* @package Laravel
* @author Taylor Otwell <taylor@laravel.com>
*/
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
Error I am getting when I run the App on xampp
Laravel App current folder structure
I've ran into this problem too. I've done some research and the only way I found possible to run Laravel on xampp was the following:
- In the parent folder of the laravel setup, I created a folder called "laravel" and moved everything inside of it.
- I went in laravel/public and took every file out of it, and I put it in the parent directory, resulting in the following folder structure: (in my htdocs)
|-.htaccess |-favicon.ico |-index.php |-robots.txt |-web.config \---laravel
- Go in index.php and change the code lines where it's loading files to load files from the new path instead (for example):
if (file_exists(__DIR__.'/laravel/storage/framework/maintenance.php')) { require __DIR__.'/laravel/storage/framework/maintenance.php'; }
- Start apache and check localhost and it should work.
这篇关于Laravel 应用程序未在 xampp 或实时服务器上运行,但在 php artisan serve 上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!