E
E
eltor2016-08-28 02:05:12
PHP
eltor, 2016-08-28 02:05:12

The controller fires after the question mark, how to fix it?

Hello. I'm studying mvc, I made a framework according to the tutorial, but there was a problem. When calling the controller For example: localhost/mvcless/news writes that Not Found
The requested URL /mvcless/news was not found on this server.
And if you call like this localhost / mvcless /? news then everything works fine
Does anyone know how to make it work without a question mark.
index.php file in the root directory

<?php

// FRONT COTROLLER

// Общие настройки

ini_set('display_errors', 1);
error_reporting(E_ALL);

// Подключение файлов системы

define('ROOT', dirname(__FILE__));
require_once(ROOT.'/components/Router.php');

// Установка соединения с БД


// Вызов Router

$router = new Router();
$router->run();

<?php

class NewsController {
  public function actionIndex()
    {

        echo 'hi';
      return true;
    }

    
}

?>

routes from config
<?php
return array(
  'news' => 'news/index', // actionIndex in NewsController
);

<?php

class Router
{

  private $routes;

  public function __construct()
  {
    $routesPath = ROOT.'/config/routes.php';
    $this->routes = include($routesPath);
  }

// Return type

  private function getURI()
  {
    if (!empty($_SERVER['REQUEST_URI'])) {
    return trim($_SERVER['REQUEST_URI'], '/');
    }
  }

  public function run()
  {
    $uri = $this->getURI();

    foreach ($this->routes as $uriPattern => $path) {

      if(preg_match("~$uriPattern~", $uri)) {

        $segments = explode('/', $path);

        $controllerName = array_shift($segments).'Controller';
        $controllerName = ucfirst($controllerName);


        $actionName = 'action'.ucfirst((array_shift($segments)));

        $controllerFile = ROOT . '/controllers/' .$controllerName. '.php';
        if (file_exists($controllerFile)) {
          include_once($controllerFile);
        }

        $controllerObject = new $controllerName;
        $result = $controllerObject->$actionName();
        if ($result != null) {
          break;
        }
      }

    }
  }
}

7caf630a5de441cab0d944ed0e273655.png
I tried to register the mvcless folder itself in virtual hosts as root, I still had to write a question mark. in windows, in xampp everything works fine. how to fix it in lamp?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladlen Hellsite, 2016-08-28
@eltor

Using NameSpace will definitely make it easier for you, and in addition, terrible router code.
As an option, you may not have everything set up correctly in .htaccess

RewriteEngine On

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

A
Alexey Aleksin, 2016-09-01
@leroyleroy

Familiar routing, I feel the tutorial from php-start :) Routing is clumsy and unfinished, if you go to a site with an address like 255.255.255.255/test/site from a server, then there will be errors, so don't be scared :) I was too lazy to finish it, so from Denver immediately uploaded it to the hosting.
By the way, all htacces from there
AddDefaultCharset utf-8
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
# Try this .htaccess setting if you have any problems rendering issues
# css styles
#AddDefaultCharset cp-1251
#ErrorDocument 404 /
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} -s [OR]
#RewriteCond %{REQUEST_FILENAME} -l [OR]
#RewriteCond %{REQUEST_FILENAME} -d
#RewriteRule ^.*$ - [L]
#RewriteRule ^.*$ index.php [L]

P
profesor08, 2016-08-28
@profesor08

The requested URL /mvcless/news was not found on this server.

This should immediately lead to the right thoughts, and the right thoughts are not related to php. The web server has no idea about your php routing. It looks for the given path from url. You pointed out the news folder to him, he did not find this folder and gave an error. Therefore, you need to convert this path to the one we will use in the php script. How to do this you have shown above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question