Skip to content

Instantly share code, notes, and snippets.

@DavertMik
Created November 11, 2012 11:49
Show Gist options
  • Save DavertMik/4054668 to your computer and use it in GitHub Desktop.
Save DavertMik/4054668 to your computer and use it in GitHub Desktop.
PHP MVC framework.
<?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();
}
}
<?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);
}
}
@DavertMik
Copy link
Author

Концепция фреймворка.
Худючий фреймворк:

  • coffeescript
  • php
  • backbone?
  • mongo/mysql/postgres

app1/
assets/
user.coffee
controllers/
UserController.php
models/
User.php
collections/
Users.php
views/
...
tests/
functional/
CreateUserCept.php
unit/
UserCest.php

Компилируется с помощью соffeescript-php + какая-то объединялка для js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment