0

I have my own MVC. In my route file I have next code:

<?php
class Route
{
    static function run()
    {
        // контроллер и действие по умолчанию
        $controller_name = 'Main';
        $action_name = 'index';

        $routes = explode('/', $_SERVER['REQUEST_URI']);

        // получаем имя контроллера
        if ( !empty($routes[1]) )
        {   
            $controller_name = $routes[1];
        }

        // получаем имя экшена
        if ( !empty($routes[2]) )
        {
            $action_name = $routes[2];
        }

        // добавляем префиксы

        $controller_name = 'Controller_'.$controller_name;
        $action_name = 'action_'.$action_name;




        // подцепляем файл с классом контроллера
        $controller_file = strtolower($controller_name).'.php';
        $controller_path = "myapp/controllers/".$controller_file;
        if(file_exists($controller_path))
        {
            include "myapp/controllers/".$controller_file;
        }
        else
        {
            /*
            правильно было бы кинуть здесь исключение,
            но для упрощения сразу сделаем редирект на страницу 404
            */
            Route::ErrorPage404();
        }

        // создаем контроллер
        $controller = new $controller_name;
        $action = $action_name;

        if(method_exists($controller, $action))
        {
            // вызываем действие контроллера
            $controller->$action();
        }
        else
        {
            // здесь также разумнее было бы кинуть исключение
            Route::ErrorPage404();
        }

    }

    function ErrorPage404()
    {
        $host = 'http://'.$_SERVER['HTTP_HOST'].'/';
        header('HTTP/1.1 404 Not Found');
        header("Status: 404 Not Found");
        header('Location:'.$host.'404');
    }
}

My core controller has next code:

<?php
class Controller {

    public $model;
    public $view;

    function __construct()
    {
        $this->view = new View();
    }

    function action_index()
    {

    }
}

?>

So, I don't know how to write BASE URL, because then I try to go on http://localhost:81/mymvc/main/index it say 404 Not Found page, and in Apache log file I have next:

[Wed Nov 26 22:30:29.481678 2014] [:error] [pid 1692:tid 780] [client 127.0.0.1:50310] PHP Fatal error:  Class 'Controller_mymvc' not found in C:\\www\\mymvc\\myapp\\base\\route.php on line 49

So how can I solve this problem? Thank you!

tereško
  • 58,060
  • 25
  • 98
  • 150
Nevada
  • 277
  • 1
  • 7
  • 13
  • Are you using configured htaccess files? – Demodave Nov 26 '14 at 21:18
  • then I create a controler with $controller = new $controller_name, it takes my base url and subfolder were I placed project. But My controller think that mymvc folder it's somecontroller. – Nevada Nov 26 '14 at 21:25
  • yes I use htaccess: DirectoryIndex /mymvc/index.php RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L] – Nevada Nov 26 '14 at 21:26
  • you have to remove leading base url string from request uri, have it in config, and if it's not blank remove it from current uri – George G Nov 27 '14 at 07:05
  • Few notes: learn to use namespaces, `explode()` is a terrible way to route .. try modifying/adapting [this](http://stackoverflow.com/a/19309893/727208) approach, stop using static methods. – tereško Nov 27 '14 at 07:21

0 Answers0