Skip to content

Instantly share code, notes, and snippets.

@odoku
Created March 29, 2012 08:02
Show Gist options
  • Save odoku/2234750 to your computer and use it in GitHub Desktop.
Save odoku/2234750 to your computer and use it in GitHub Desktop.
DjangoみたいなPHPフレームワークが欲しい。
<?php
//
// Routing.php
// ===========================================================================+
return urlpatterns(array(
url('^$', 'content.view.index', 'index'),
include_urlpatterns('^/friend/', 'content.urls', 'friend'),
));
//
// views.php
// ===========================================================================+
class Index extends View {
public function get($request) {
$context = array();
$response = new HttpResponse();
$response->body = template('/templates/index.html', $context);
return $response;
}
public function post($request) {
$context = array();
if ($request->method !== 'POST') {
throw new Http404();
// throw new HttpClientError(404, 'Not Found');
}
$form = new SampleForm($response->post, $response->files);
if ($form->is_valid()) {
$form->save();
return new HttpResponseRedirect('/');
}
$response = new HttpResponse();
$response->body = template('/templates/index.html', $context);
return $response;
}
}
//
// forms.php
// ===========================================================================+
class SampleForm extends Form {
protected function fields() {
$name = $this->create_field('name', 'text');
$name->required = true;
$name->max_length = 255;
$name->attributes = array(
'id' => 'Name',
'class' => 'text_input'
)
$url = $this->create_field('url', 'url');
$url->required = true;
$url->max_length = 2048;
$url->attributes = array(
'id' => 'Url',
'class' => 'text_input'
)
}
protected function clean_name() {
$name = $this->name->value();
$ng_words = array('hoge', 'piyo', 'foo', 'bar');
if (in_array($name, $ng_words)) {
throw new ValidationError('NGワードが含まれています。');
}
}
public function save() {
// セーブ処理
}
}
?>
//
// templates/base.html
// ===========================================================================+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Framework</title>
</head>
<body>
<div id="content">
<?php echo block('content') ?>
</div>
</body>
</html>
//
// templates/index.html
// ===========================================================================+
<?php extend_template('/templates/base.html') ?>
<?php open_block('content') ?>
<h1>Wonderful Framework like Django python!</h1>
<?php close_block() ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment