Answer the question
In order to leave comments, you need to log in
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;
}
}
?>
<?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;
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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]
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]
The requested URL /mvcless/news was not found on this server.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question