'.$this->params['controller'].'
';
echo '
Action: '.$this->params['action'].'
';
}
else {
echo "Marshurt topilmadi";
}
}
Endi bosh sahifaning urlini topishimiz kerak. Routes.php
'' => [
'controller' => 'main',
'action' => 'index',
],
Endi biz controllerni alohida ajratib olamiz va controller mavjud yoki mavjud emasligini tekshiramiz. Router.php
public function run()
{
if($this->match())
{
$controller = 'app\controllers\\'.$this->params['controller'];
echo $controller;
}
else {
echo "Marshurt topilmadi";
}
}
public function run()
{
if($this->match())
{
$controller = 'app\controllers\\'.ucfirst($this->params['controller']).'Controller.php';
echo $controller;
}
else {
echo "Marshurt topilmadi";
}
}
public function run()
{
if($this->match())
{
$controller = 'app\controllers\\'.ucfirst($this->params['controller']).'Controller.php';
if(class_exists($controller)){
echo "Ok";
}
else {
echo "Controller topilmadi: ".$controller;
}
}
else {
echo "Marshurt topilmadi";
}
}
Actionni ham aniqlab olamiz.
public function run()
{
if($this->match())
{
$controller = 'app\controllers\\'.ucfirst($this->params['controller']).'Controller.php';
if(class_exists($controller)){
$action = $this->params['action'].'Action';
if(method_exists($controller, $action)) {
}
else {
echo 'Metod topilmadi: '.$action;
}
}
else {
echo "Controller topilmadi: ".$controller;
}
}
else {
echo "Marshurt topilmadi";
}
}
public function run()
{
if($this->match())
{
$path = 'app\controllers\\'.ucfirst($this->params['controller']).'Controller';
if(class_exists($path)){
$action = $this->params['action'].'Action';
if(method_exists($path, $action)) {
$controller = new $path;
$controller->$action();
}
else {
echo 'Metod topilmadi: '.$action;
}
}
else {
echo "Controller topilmadi: ".$path;
}
}
else {
echo "Marshurt topilmadi";
}
}
Yangi controller yaratamiz. App->controllers->AccountController.php
namespace app\controllers;
class AccountController
{
public function loginAction()
{
echo "Login sahifa";
}
public function registerAction()
{
echo "Register sahifa";
}
}
Controllerning bazaviy classini yaratamiz. App->core->Controller.php
namespace app\core;
abstract class Controller
{
public function __construct()
{
echo "Controller classi";
}
}
Bu classdan AccountController classiga meros olamiz.
namespace app\controllers;
use app\core\Controller;
class AccountController extends Controller
{
public function loginAction()
{
echo "Login sahifa";
}
public function registerAction()
{
echo "Register sahifa";
}
}
Router.php ning run metodi ichidagi $controller o’zgaruvchi obyektiga parameter kiritamiz va uni Controller.php fayliga constructor metodiga yuboramiz.
$controller = new $path($this->params);
Controller.php
namespace app\core;
abstract class Controller
{
public $route;
public function __construct($route)
{
var_dump($route);
}
}
public function __construct($route)
{
$this->route = $route;
}
Route xossasini controllerdan meros olayotgan istalgan controllerda ishlatishimiz mumkin. AccountController.php
public function registerAction()
{
echo "Register sahifa";
var_dump($this->route);
}
MainController.php faylini yaratamiz.
namespace app\controllers;
use app\core\Controller;
class MainController extends Controller
{
public function indexAction()
{
echo "Bosh sahifa";
}
}
App->core->View.php faylini yaratamiz. Bu fayl bizga sayt dizayni bilan ishlash uchun kerak bo’ladi.
namespace app\core;
class View
{
public $path;
public $layout = 'default';
}
App->views->account/layouts/main kataloglarini yaratamiz. Layouts->default.php faylini yaratib olamiz.
Title
Do'stlaringiz bilan baham: |