Skip to content

Instantly share code, notes, and snippets.

@mcihad
Last active October 22, 2019 18:10
Show Gist options
  • Save mcihad/b3897367bdc85910f8cc to your computer and use it in GitHub Desktop.
Save mcihad/b3897367bdc85910f8cc to your computer and use it in GitHub Desktop.
PHP MVC Basit Url Yönlendirmesi ve Action Çalıştırma, Simple MVC implementation , Url Routing and action execution
<?php
class Controller {
protected $data=array();
public function set($name,$value) {
$this->data[$name]=$value;
}
public function get($name) {
if (array_key_exists($name, $this->data)){
return $this->data[$name];
}
return false;
}
public function redirect($url) {
header("location:/".$url);
}
public function render($template) {
if (preg_match("/[\/a-zA-Z\-\_]/", $template) &&
file_exists(dirname(__FILE__).'/'.$template.'.html')) {
ob_start();
extract($this->data);
include dirname(__FILE__).'/'.$template.'.html';
$data=ob_get_contents();
ob_end_clean();
return $data;
} else {
return "Template Not Found:".$template.'.html<br />';
}
}
public function getMethod() {
return $_SERVER['REQUEST_METHOD'];
}
public function isPost() {
return strtoupper($this->getMethod())=='POST';
}
public function isGet() {
return strtoupper($this->getMethod())=='GET';
}
public function getPost($name) {
return isset($_POST[$name])? $_POST[$name] : null;
}
}
class UserController extends Controller {
public function login() {
echo $this->render("login");
}
}
class ProductController extends Controller {
public function beforeAction() {
if ($this->get("id")>100) {
$this->redirect("musteriler/cihad-gundogdu");
}
}
public function show() {
echo 'Product Name:'.$this->get("name").'<br />';
echo 'Product Code:'.$this->get("id").'<br />';
}
public function afterAction() {
echo '<br />';
echo 'After Action';
}
}
class MusteriController extends Controller {
public function beforeAction() {
#if(!$this->get("login_test")) {
# $this->redirect("user/login");
#}
}
public function index() {
echo $this->get("name");
}
}
class KategoriController extends Controller {
public function show() {
echo $this->get("id");
}
}
$urls=array(
#?q=musteriler/cihad-gundogdu
#musteriler/cihad-gundogdu
#?q=musteriler/deneme-deneme_1250
#musteriler/deneme-deneme_1250
'/^user\/login$/'=>'User#login',
'/^musteriler\/(?P<name>[a-zA-Z0-9\-\_]+)$/'=>'Musteri#index',
#product/audi-a4-2000-tdi_1250.html
'/^product\/(?P<name>[a-zA-Z0-9\-\_]+)\_(?P<id>\d+)\.html$/'=>'Product#show',
#?q=kategori/1250
#kategori/1250
'/^kategori\/(?<id>\d+)$/i'=>'Kategori#show'
);
$url=isset($_GET['q']) ? $_GET['q'] : '';
$matched=false;
foreach($urls as $pattern => $action) {
if (preg_match($pattern, $url,$matches)) {
$actionMap=explode("#", $action);
$controller=$actionMap[0];
$action=$actionMap[1];
$matched=true;
break 1;
}
}
if (!$matched) {
echo '404 Page Not Found';
} else {
$params=array();
foreach ($matches as $key => $value) {
if (!is_int($key)) {
$params[$key]=$value;
}
}
$cTemp=$controller;
$controller=$controller.'Controller';
$ctrl=new $controller();
foreach ($params as $key => $value) {
$ctrl->set($key,$value);
}
$ctrl->set("controller",$cTemp);
$ctrl->set("action",$action);
if (method_exists($ctrl, "beforeAction")) {
$ctrl->beforeAction();
}
$ctrl->{$action}();
if (method_exists($ctrl, "afterAction")) {
$ctrl->afterAction();
}
}
#----------------------login.html-----------------------------
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Kullanıcı Girişi</title>
</head>
<body>
<h3><?= $controller ?>#<?= $action ?></h3>
<form action="/user/login" method="post">
<div class="element">
<label for="username">Kullanıcı Adı</label>
<input type="text" name="username" id="">
</div>
<div class="element">
<label for="password">Password</label>
<input type="password" name="password" id="">
</div>
<div class="submit">
<input type="submit" value="Kaydet">
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment