Laravel 应用程序不在 xampp 或实时服务器上运行,但在 php artisan serve 上运行良好
问题描述
我的 laravel 应用无法在 xampp 或实时服务器上运行,但在 PHP artisan serve 提供的链接上运行良好.每当我在 xampp 上运行此应用程序时,它都会返回 404 错误.我还有其他 laravel 应用程序也可以在 xampp 上正常工作,但是这个.我找不到任何解决方案来解释为什么我的 laravel 没有在托管服务器或 xampp 上运行.我已经尝试了相关问题中找到的所有解决方案,但没有发现任何有用的解决方案.
- 我尝试在更改现有的 .htaccess 后运行该应用程序根文件夹中的文件.
- 我尝试在公开更改 .htaccess 文件后运行该应用程序文件夹.
- 我尝试在删除其中一个后运行该应用.
- 我已尝试使用再次安装和更新依赖项作曲家.
我应该怎么做才能让它运行?
当我尝试通过 xampp 运行它时看到的是错误,但是当通过它运行 Web 应用程序时总是出现的文件夹结构.
现在,我的根文件夹中有一个 htaccess 文件.下面是它的代码:
我的 composer.json 文件的代码:
我的 package.json 文件的代码:
我的 server.php 文件的代码:
我的 index.php 文件在 public 文件夹中的代码:
我在 xampp 上运行应用程序时遇到的错误
Laravel App 当前文件夹结构
解决方案
我也遇到过这个问题.我做了一些研究,发现在 xampp 上运行 Laravel 的唯一方法是:
在 laravel 设置的父文件夹中,我创建了一个名为laravel"的文件夹.并将所有内容移到其中.
我进入 laravel/public 并从中取出每个文件,并将其放在父目录中,从而得到以下文件夹结构:(在我的 htdocs 中)
<块引用>
进入 index.php 并将加载文件的代码行更改为从新路径加载文件(例如):
<块引用>
启动 apache 并检查 localhost,它应该可以工作.
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(IlluminateContractsHttpKernel::class);
$response = $kernel->handle(
$request = IlluminateHttpRequest::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 上运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!