apache / laragon+作曲家自动加载问题

发布于 2025-02-10 17:50:38 字数 2184 浏览 3 评论 0原文

我刚刚开始尝试创建一个简单的路由器(我尝试了几种解决方案,例如Altorouter,而没有奏效,我想启动一个非常简单的解决方案)。 作曲家自动加载无法达到命名空间。

致命错误:未被发现的错误:c:\ laragon \ www \ router \ router \ index.php中找不到类'app \ router' \ index.php在第6行

我做了“作曲家转储 - autoload”和“作曲家更新命令”,甚至尝试了一个自制的自动加载器。

这是我项目的架构:

  • 路由器
  • src

  •  >路由器
     
  •  路由器
     
  •  路线  
     
  • 供应商

  •  自动加载
     
  • .htaccess
  • index.php
  • composer.json
  • composer.lock

作曲家

{
    "name": "root/router",
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "authors": [
        {
            "name": "********",
            "email": "************"
        }
    ],
    "require": {}
}

这是

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

<?php
require './vendor/autoload.php';

use App\Router;

$router = new Router($_GET[$url]);

$router->get('/home', function(){ echo 'welcome on home page'; });

$router->run();

<?php
namespace App;

use App\Route;

class Router {

    public $url;
    private $routes = [];

    public function __construct($url)
    {
        $this->url = $url;
    }

    public function get($path, $callable)
    {
        $route = new Route($path, $callable);
        $this->routes['GET'][] = $route;
    }

    public function post($path, $callable)
    {
        $route = new Route($path, $callable);
        $this->routes['POST'][] = $route;
    }

    public function run()
    {
        echo '<pre>';
        echo print_r($this->routes);
        echo '</pre>';
    }
}

<?php
namespace App;

class Route {

    public function __construst($path, $callable)
    {
        $this->path = $path;
        $this->callable = $callable;
    }
}

I just started an attempt to create a simple Router (I tried several solutions like AltoRouter and as nothing worked, I wanted to started a very simple one).
The Composer autoload can't reach the namespace.

Fatal error: Uncaught Error: Class 'App\Router' not found in C:\laragon\www\router\index.php:6 Stack trace: #0 {main} thrown in C:\laragon\www\router\index.php on line 6

I did the "composer dump-autoload" and the "composer update commands" and even tried an home made autoloader.

Here is the architecture of my project :

  • router
  • src

  •   > Router
    
  •      Router
    
  •      Route  
    
  • vendor

  •   autoload
    
  • .htaccess
  • index.php
  • composer.json
  • composer.lock

Here is my composer.json

{
    "name": "root/router",
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "authors": [
        {
            "name": "********",
            "email": "************"
        }
    ],
    "require": {}
}

Here is my .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Here is my index.php

<?php
require './vendor/autoload.php';

use App\Router;

$router = new Router($_GET[$url]);

$router->get('/home', function(){ echo 'welcome on home page'; });

$router->run();

Here is my Router class

<?php
namespace App;

use App\Route;

class Router {

    public $url;
    private $routes = [];

    public function __construct($url)
    {
        $this->url = $url;
    }

    public function get($path, $callable)
    {
        $route = new Route($path, $callable);
        $this->routes['GET'][] = $route;
    }

    public function post($path, $callable)
    {
        $route = new Route($path, $callable);
        $this->routes['POST'][] = $route;
    }

    public function run()
    {
        echo '<pre>';
        echo print_r($this->routes);
        echo '</pre>';
    }
}

Here is my Route.php class

<?php
namespace App;

class Route {

    public function __construst($path, $callable)
    {
        $this->path = $path;
        $this->callable = $callable;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2025-02-17 17:50:38

我认为问题可能是命名空间。
尝试以下操作:

namespace App\Router

然后键入作曲家dump-autoload

I think the problem might be the namespace.
Try this:

namespace App\Router

then type composer dump-autoload

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文