Created
November 11, 2012 11:49
-
-
Save DavertMik/4054668 to your computer and use it in GitHub Desktop.
PHP MVC framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Users extends DataCollection { | |
// mapping | |
public static $name = 'name'; | |
public static $homepage = 'url'; | |
public static $email = 'email'; | |
public static $created_at = 'created_at'; | |
protected function validate($data) | |
{ | |
$rules = $this->createValidator($data); | |
$rule(self::$name)->type('string')->required(); | |
$rule(self::$email)->type('email'); | |
$rule(self::$homepage)->type('url')->required(); | |
return $rule->getData(); | |
} | |
public function create($userData) | |
{ | |
if (!$this->validate($userData)) throw $this->getValidationError(); | |
$user = new User($userData); | |
$user->save(); | |
return $user; | |
} | |
public funcion delete($id) | |
{ | |
$user = $this->fetch($id); | |
$user->delete(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Framework concept | |
// Very THIN controller + THICK model | |
// Great for MVC frontend apps | |
// Using models from frontend is just like using from backend | |
class UsersController extends Users { | |
// ROUTE: /users | |
private function action($request, $user) | |
{ | |
if ($request->GET('/')) | |
return new JsonResponse($this->list($request['offset'])); | |
if ($request->GET('/:id')) | |
return new JsonResponse($this->fetch($request['id'])); | |
if ($request->POST('/') and $user->is('admin')) | |
return new RawMessageResponse($this->create($request['user']), 'user was sucessfully created'); | |
return 404; | |
} | |
function list($offset = 0) | |
{ | |
return $this | |
->where(array('user_id', $user->id)) | |
->limit(15) | |
->offset($offset) | |
->ascOrder('created_at DESC') | |
} | |
function fetch($id) | |
{ | |
return parent::fetch($id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Концепция фреймворка.
Худючий фреймворк:
app1/
assets/
user.coffee
controllers/
UserController.php
models/
User.php
collections/
Users.php
views/
...
tests/
functional/
CreateUserCept.php
unit/
UserCest.php
Компилируется с помощью соffeescript-php + какая-то объединялка для js