【问题标题】:The requested URL was not found on this server when using PHP Mamp, Xamp使用 PHP Wamp、Xampp 时在此服务器上找不到请求的 URL
【发布时间】:2020-08-31 01:46:29
【问题描述】:

我使用 Mamp 创建 PHP 服务器。 默认主页是 homecontroller。但是当我点击指向主页或任何 url 的 url 时,出现问题:在此服务器上找不到请求的 URL /ltw/home。当我替换受保护的 $controller = "home";通过任何控制器,它可以在我进入索引页面时加载。

 class App
{
    protected $controller = "home";
    protected $action = "default";
    protected $params = [];
    
   public function __construct()
    {
        //Array ( [0] => home [1] => 1 [2] => 2 [3] => 3 )
        $arr = $this->UrlProcess();
       // print_r($arr);

        //Handle Controller
        if (file_exists("./mvc/controllers/". $arr[0] ."Controller.php")){
            $this->controller = $arr[0];
            unset($arr[0]);
        } 
        require_once "./mvc/controllers/" . $this->controller . "Controller.php";
        $this->controller = new $this->controller;

        //Handle Action
        if (isset($arr[1]) && method_exists($this->controller, $arr[1])){
            $this->action = $arr[1];
            unset($arr[1]);
        }

        //Handle Params
        $this->params = $arr?array_values($arr):[];
        $arr2 = array($this->params);
        
        //Call a controller->action->params
        call_user_func_array([ $this->controller, $this->action ], $arr2 );
        // print_r($this->params);
    }

directory structure

这个错误只发生在mac和xampp上,

【问题讨论】:

    标签: php xampp mamp


    【解决方案1】:

    我建议您在 .htacess 根目录中使用:

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

    如果没有帮助,您应该将主控制器修改为:

    <?php
    
    class Core
    {
        public function start() {
       
        $url = '/';
        if (isset($_GET['url'])) {
            $url .= $_GET['url'];
        }
    
       
        $params = array();
    
        if (!empty($url) && $url != '/') {
            $url = explode('/', $url);
            
            array_shift($url);
    
            $currentController = $url[0].'Controller';
            
            array_shift($url); 
    
            if (isset($url[0]) && !empty($url[0])) {
                $currentAction = $url[0];
                array_shift($url);
            } else {
                $currentAction = 'index';
            }
    
            if (count($url) > 0) {
                 $params = $url;
            }
     
        } else {
            $currentController = 'homeController';
            $currentAction = 'index';
        }
    
        
        if ( !(file_exists('controllers/'.$currentController.'.php') || file_exists('controllersReport/'.$currentController.'.php')) || !method_exists($currentController, $currentAction)) {
            $currentController = 'notfoundController';
            $currentAction = 'index';
        } 
    
        $c = new $currentController();
        call_user_func_array(array($c, $currentAction), $params);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2019-02-08
      • 2017-03-21
      • 2010-10-16
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      相关资源
      最近更新 更多