【问题标题】:Laravel App not running on xampp or live server but works well on php artisan serveLaravel 应用程序不在 xampp 或实时服务器上运行,但在 php artisan serve 上运行良好
【发布时间】:2021-08-21 18:18:24
【问题描述】:

我的 laravel 应用无法在 xampp 或实时服务器上运行,但在 PHP artisan serve 提供的链接上运行良好。每当我在 xampp 上运行此应用程序时,它都会返回 404 错误。我还有其他 laravel 应用程序也可以在 xampp 上正常工作,但是这个。 我找不到任何解决方案来解释为什么我的 laravel 没有在托管服务器或 xampp 上运行。我已经尝试了相关问题中的所有解决方案,但没有发现任何有用的解决方案。

  • 我尝试在更改现有的 .htaccess 后运行该应用程序 根文件夹中的文件。
  • 我尝试在公开更改 .htaccess 文件后运行该应用程序 文件夹。
  • 我尝试在删除其中一个后运行该应用程序。
  • 我尝试使用再次安装和更新依赖项 作曲家。

我应该怎么做才能让它运行?

当我尝试通过 xampp 运行它时,我看到的是错误,但是当通过它运行 Web 应用程序时总是出现的文件夹结构。

现在,我的根文件夹中有一个 htaccess 文件。下面是它的代码:

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

我的 composer.json 文件的代码:

{
    "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"
        ]
    }
}

我的 package.json 文件的代码:

{
    "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"
    }
}

我的 server.php 文件的代码:

<?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';

我的 index.php 文件在 public 文件夹中的代码:

<?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);

我在 xampp 上运行应用程序时遇到的错误

Laravel App 当前文件夹结构

【问题讨论】:

  • 您需要编辑apache配置,将DocumentRoot指向laravel_root/public

标签: php laravel mongodb .htaccess xampp


【解决方案1】:

我也遇到过这个问题。我做了一些研究,发现在 xampp 上运行 Laravel 的唯一方法是:

  1. 在 laravel 设置的父文件夹中,我创建了一个名为“laravel”的文件夹并将所有内容移到其中。
  2. 我进入 laravel/public 并从中取出每个文件,并将其放在父目录中,从而得到以下文件夹结构:(在我的 htdocs 中)
|-.htaccess  
|-favicon.ico  
|-index.php  
|-robots.txt  
|-web.config   
\---laravel
  1. 进入 index.php 并将加载文件的代码行更改为从新路径加载文件(例如):
if (file_exists(__DIR__.'/laravel/storage/framework/maintenance.php'))   
{  
    require __DIR__.'/laravel/storage/framework/maintenance.php';  
}
  1. 启动 apache 并检查 localhost,它应该可以工作。

【讨论】:

  • 非常感谢,它确实解决了我的问题。在那之后,为了弄清楚实际问题可能是什么,我把我的整个项目改回了之前问这个问题时的样子,然后更改了我的应用程序文件夹的名称,这样做也解决了这个问题。
  • 不客气,伙计!但是请注意,虽然此解决方案是这样工作的,但由于公用文件夹已移出,您将无法再使用“php artisan serve”启动 Laravel。
猜你喜欢
  • 2020-08-21
  • 2019-11-22
  • 2013-06-08
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
相关资源
最近更新 更多