Tak jak w temacie, chciałbym jakoś prosto rozwiązać sprawę nice url żeby z takiego linku:
www.xx.pl/gallery/show/12
wyciągnąć i odpalić po kolei:
kontroler: gallery
metoda: show
parametr: 12
do tej pory osiągnąłem takie coś:
<?php
//include('config.php');
include('application/SimpleUrl.class.php');
function classAutoloader($class)
{
include ('controllers/'.$class.'.controller.php');
}
spl_autoload_register('classAutoloader');
$url = new SimpleUrl('/labs/mvc3');
$url_controller = $url->segment(1);
$url_action = $url->segment(2);
$url_param = $url->segment(3);
if ($url_controller)
{
$controller = new $url_controller;
if ($url_action && $url_param)
{
$controller->$url_action($url_param);
}
elseif ($url_action)
{
$controller->$url_action();
}
} else {
$controller = new Home;
}
?>
oraz simpleUrl:
<?php
class SimpleUrl
{
var $site_path;
public function __construct($site_path)
{
$this->site_path = $this->removeSlash($site_path);
}
public function __toString()
{
return $this->site_path;
}
private function removeSlash($string)
{
if ($string[strlen($string) - 1] == '/') {
$string = rtrim($string, '/'); }
return $string;
}
public function segment($segment)
{
$url = str_replace($this->site_path, '', $_SERVER['REQUEST_URI']); if (isset($url[$segment])) {
return $url[$segment];
} else {
return false;
}
}
}
?>
Teraz to działa, ale tylko jeśli plik nazywa się Home.controller.php, a ja chciałbym zrobić tak:
$controller = new HomeController;
żeby ta część ładowała mi plik Home.controller.php
a w linku wyświetlało się www.xx.pl/home
da się to zrobić ?
Ten post edytował Szymciosek 9.07.2012, 22:18:41