MVC view-controller

530 Views Asked by At

I start to learning MVC and I write my own MVC pattern, and I can do only main-controller and main-view, but I can't understand how to make another controller/action and I want to make some link from my main-view to another page. So I have next folders and and next simle code: In my index.php I have simple:

<?php
ini_set('display_errors',1);
require_once 'myapp/bootstrap.php';

Next, in my bootstrap.php I connect my base classes view.php, controller.php, route.php and I run the Route function run():

<?php
require_once 'base/view.php';
require_once 'base/controller.php';
require_once 'base/route.php';
include_once 'Numbers/Words.php';
Route::run(); //start routing
?>

In my route.php I write this function run()

<?php
class Route
{
    static function run()
    {
        // controller and action by defalt
        $controller_name = 'Main';
        $action_name = 'index';

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

        // get controller name
        if ( !empty($routes[1]) )
        {   
            $controller_name = $routes[1];
        }

        // get action name
        if ( !empty($routes[2]) )
        {
            $action_name = $routes[2];
        }

        // add prefix

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




        // add file with controller class
        $controller_file = strtolower($controller_name).'.php';
        $controller_path = "myapp/controllers/".$controller_file;
        if(file_exists($controller_path))
        {
            include "myapp/controllers/".$controller_file;
        }
        else
        {
            Route::ErrorPage404();
        }

        // create controller
        $controller = new $controller_name;
        $action = $action_name;

        if(method_exists($controller, $action))
        {
            // invoke action of controller
            $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');
    }
}

It is defines my controllers and acrions routes. And I also have my Controller_Main:

<?php
class Controller_Main extends Controller
{
    function action_index()
    {   
        $this->view->generate('main_view.php', 'template_view.php');
    }
}

It loads my view and tamplate:

<div class="title">
<h1>Paymentwall PHP Test</h1>
<h2>Number To String Convertion</h2>
    </div>
    <div class="convertion_form">
<form name="form" class="form" method="POST" action="main/index">
    <label>Enter your Number Please:</label>
    <input class="number_input" type="text" name="number_input">
    <input type="submit" value="Convert">
</form>
    </div>

Tamplate:

<!DOCTYPE html>
<html>
<head>
<title>Main Page</title>
<link rel="stylesheet" href="http://localhost:81/css/style.css">
<meta charset="utf-8">
</head>
<body>
    <?php include 'myapp/views/'.$content_view; ?>
</body>
</html>

So, my question is - what I need to do in my route.php to create another controller with action, and load another veiw? And how to write a link in my Main_View to another view? And I also have some web form, what I need to write in action="" ??? Please help me because I can not understand myself and find the answer.

1

There are 1 best solutions below

1
On

You can create another action in your controller like this:

public function action_submit()
{
     $this->view->generate('blabla');
}

And link it as /main/submit or you can create a new controller file and put some actions in it. Anyway look into some frameworks, CodeIgniter would be good for beginner, but don't stop on it once you understand how it works you can learn more complex ones, eventually coming to Symfony2/ZF2.

Edit: Actually better learn on your mistakes first, it will give you much better in-depth knowledge. And about frameworks - replace CodeIgniter (yeah it's shit, I just remember that I was learning with it at my first steps) with Silex.