-1

When i type this address:

http://localhost/gamelvl/world-of-tanks/tankguide/

I can enter the tankguide view. Now inside this folder is another folder called ussr but when I create a controller and model for ussr I can not enter it with this address:

http://localhost/gamelvl/world-of-tanks/tankguide/ussr/

Now, any one can give me instructions on whether this is the right thing or other solutions for this?

routing app

    <?php

class App
{
    public $controller = 'index';
    public $method = 'index';
    public $params = [];

    function __construct()
    {
        if (isset($_GET['url'])) {
            $url = $_GET['url'];
            $url = $this->parseUrl($url);
            $this->controller = $url[0];
            unset($url[0]);
            if (isset($url[1])) {
                $this->method = $url[1];
                unset($url[1]);
            }
            $this->params = array_values($url);
        }
        $controllerurl = 'controllers/' . $this->controller . '.php';
        if (file_exists($controllerurl)) {
            require($controllerurl);
            $object = new $this->controller;
            $object->model($this->controller);
            if (method_exists($object, $this->method)) {
                call_user_func_array([$object, $this->method], $this->params);
            }
        }
    }

    function parseUrl($url)
    {
        filter_var($url, FILTER_SANITIZE_URL);
        $url = rtrim($url, '/');
        $url = explode('/', $url);
        return $url;
    }
}

?>

tankguide controller

<?php

class Tankguide extends Controller
{
       function index()
    {
        $this->view('tankguide/index');
    }
}

?>

core controller

 <?php

class Controller
{
    function __construct()
    {

    }

    function view($viewUrl,$data=[])
    {
        require('header.php');
        require('views/' . $viewUrl . '.php');
        require('footer.php');
    }

    function model($modelUrl)
    {
        require('models/model_' . $modelUrl . '.php');
        $classname = 'model_' . $modelUrl;
        $this->model = new $classname;
    }
}

?>

project structure

enter image description here

Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
BlackHill
  • 1
  • 1
  • 6
  • `if (method_exists($object, $this->method))` will return false for the given url, because it is looking for a method called `ussr` on the `Tankguide` class. Either create a `ussr` method on that class, or use a more flexible router that doesnt link url to filesystem. A google for `php router` will reveal plenty – Steve Sep 11 '17 at 19:48
  • when i create ussr method in Tankguide class how i can show HTML and CSS codes for ussr method ? – BlackHill Sep 11 '17 at 19:52
  • presumably the exact same way you do in your other routes: `function ussr(){$this->view('tankguide/ussr');}` – Steve Sep 11 '17 at 20:14
  • you mean `function ussr(){$this->view('tankguide/ussr/index');}`? – BlackHill Sep 11 '17 at 20:19
  • If you have created a subfolder called `ussr` and created a 'view' file called `index.php` within it, then yes, though it would make more sense to just create a `ussr.php` view directly in the `tankguide` folder - is this your own homemade mvc framework? – Steve Sep 11 '17 at 20:22
  • yes and ussr is child folder for tankguide! – BlackHill Sep 11 '17 at 20:28
  • I understand the relationship between ussr and tank, im just not sure why you need to create a separate folder with an index file for each 'faction' when named files would suffice (and in fact be a lot clearer, having numerous `index.php` files in your project is going to get confusing fast) – Steve Sep 11 '17 at 20:38
  • Im also a little concerned that i could understand the router you wrote, but you couldnt. If you are just starting out with MVC, you would probably be a lot better off picking a well documented framework with an active community. Laravel has a huge community (and a solid video tutorial site in Laracasts), but is a complex framework witha good deal of 'magic'. Fatfreephp is much smaller, but still has a good community and is a lot easier to pick up for a beginner – Steve Sep 11 '17 at 20:49
  • @Komeil you might want to read this: https://stackoverflow.com/a/19309893/727208 – tereško Sep 12 '17 at 07:56

1 Answers1

0

if (method_exists($object, $this->method)) will return false for the given url, because it is looking for a method called ussr on the Tankguide class. Either create a ussr method on that class

BlackHill
  • 1
  • 1
  • 6