MVC の学習を開始し、独自の MVC パターンを作成しました。メインコントローラーとメインビューしか実行できませんが、別のコントローラー/アクションを作成する方法が理解できず、メインからリンクを作成したいと考えています。別のページを表示します。だから私は次のフォルダと次の単純なコードを持っています: 私の index.php には単純なものがあります:
<?php
ini_set('display_errors',1);
require_once 'myapp/bootstrap.php';
次に、bootstrap.php で基本クラスの view.php、controller.php、route.php を接続し、Route 関数 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
?>
私の route.php では、この関数 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');
}
}
それは私のコントローラーと acrions ルートを定義しています。また、Controller_Main もあります。
<?php
class Controller_Main extends Controller
{
function action_index()
{
$this->view->generate('main_view.php', 'template_view.php');
}
}
ビューと 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>
タンプレート:
<!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>
だから、私の質問は - アクションで別のコントローラーを作成し、別のビューをロードするには、route.php で何をする必要がありますか? Main_View に別のビューへのリンクを書き込む方法は? そして、私はいくつかのWebフォームも持っています。何を書く必要がありますaction=""
か??? 私は自分自身を理解して答えを見つけることができないので、私を助けてください。