CMS-Routing in MVC
Ich erstelle mein eigenes MVC-Framework in PHP, um fortgeschrittenere Programmierkenntnisse zu erlernen. Ich habe das Framework eingerichtet und ausgeführt, habe jedoch ein Problem mit der aktuellen Routing-Methode. Ich möchte, dass das Framework ein Backend-CMS unterstützt, um die Front-End-Website zu ergänzen. Das Problem ist, dass meine Routing-Struktur so funktioniert -mywebsite.com/controller/method/id
Das Routingmodul sortiert die Informationen in ein Array wie dieses
segments 0 => controller, 1 => method, 2 => id
Wenn ich jetzt mywebsite.com/projects besuche, gehe ich zu der Seite, die ich als Administrator eingerichtet habe. Ich möchte nicht nur, dass dies nur über mywebsite.com/admin/projects zugänglich ist, sondern dass die mywebsite.com/projects mich zum Frontend bringen.
Also wenn ich besuchen willmywebsite.com/projects
Ich möchte, dass es den "Front" -Controller rendert und "Projekte" in die Methode einbindet. Wenn ich besuchemywebsite.com/admin/projects
Ich möchte, dass der Projektcontroller geladen wird.
Hier ist die aktuelle Routing-Klasse insgesamt wie folgt.
<?php
class Request {
//url domain.com/controller/method/other
//holds url segments 0 => controller, 1 => method, 2 => other, etc
public $segments;
function __construct() {
$this->parse_globals();
}
function parse_globals(){
//$uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
$uri = (empty($_GET['rt'])) ? '' : $_GET['rt'];
$this->segments = array_filter(explode('/', $uri));
if (in_array("admin", $this->segments)) {
array_shift($this->segments);
}
print_r($this->segments);
//remove index php
if( reset($this->segments) == 'index.php') {
array_shift ($this->segments);
}
}
function controller(){
return $this->segment(0);
}
function method(){
return $this->segment(1);
}
function param( $str ){
return isset($_REQUEST[$str]) ? $_REQUEST[$str] : false;
}
function segment( $no ){
return isset($this->segments[$no]) ? $this->segments[$no] : false;
}
}