Weitere ähnliche Inhalte Ähnlich wie Building Lithium Apps (20) Kürzlich hochgeladen (20) Building Lithium Apps20. index.php?url=/posts
config/bootstrap.php
config/bootstrap/libraries.php
/posts config/bootstrap/cache.php
config/bootstrap/connections.php
config/bootstrap/action.php
...
21. index.php?url=/posts
config/bootstrap.php
config/bootstrap/libraries.php
/posts config/bootstrap/cache.php
config/bootstrap/connections.php
config/bootstrap/action.php
...
lithiumactionDispatcher::run(
new lithiumactionRequest()
)
22. config/bootstrap/cache.php
Cache::config(array(
'local' => array(
'adapter' => 'Apc'
),
'distributed' => array(
'adapter' => 'Memcached',
'servers' => array(
array('127.0.0.1', 11211, 100)
array('127.0.0.2', 11211, 100)
)
)
));
23. config/bootstrap/connections.php
Connections::add('default', array(
'development' => array(
'type' => 'MongoDb',
'host' => 'localhost',
'database' => 'foo'
),
'production' => array(
'type' => 'MongoDb',
'host' => 'db.host',
'database' => 'foo.prod'
)
));
Connections::add('cassandra', array(
'type' => 'Cassandra',
'keyspace' => 'foo'
));
29. There are no routes here yet
lithiumactionDispatcher {
//...
public static function run($request, ...) {
// Pass the request to the Router
$params = $classes['router']::process($request);
// Do some checking...
// Get a controller...
return static::call($controller);
}
}
31. lithiumactionDispatcher {
//...
public static function run($request, ...) {
// Pass the request to the Router
$params = $classes['router']::process($request);
// Do some checking...
// Get a controller...
// $controller = ...
return static::call($controller);
}
}
Dispatcher::applyFilter('run', function($self, $params, $chain) {
foreach (Libraries::get() as $name => $config) {
// Some sanity checking goes here...
include "{$config['path']}/config/routes.php";
}
return $chain->next($self, $params, $chain);
});
34. class WeblogController < ActionController::Base
def index
@posts = Post.find :all
respond_to do |format|
format.html
format.xml { render :xml => @posts.to_xml }
format.rss { render :action => "feed.rxml" }
end
end
end
35. class WeblogController < ActionController::Base
def index
@posts = Post.find :all
respond_to do |format|
format.html
format.xml { render :xml => @posts.to_xml }
format.rss { render :action => "feed.rxml" }
end
end
end
36. lithiumnethttpMedia {
$formats = array(
array( 'html' => array(...),
'posts' => ... 'json' => array(...),
) 'xml' => array(...),
'...'
);
}
38. Dispatcher index.php
Controller
new lithiumactionResponse()
View
41. /**
* This handles both the home page and the archives pages.
*/
Router::connect('/{:page}', array('page' => 1), array(
'pattern' => '@^/?(?P<page>d+)$|^/?$@',
'keys' => array('page' => true),
'handler' => function($request) use ($render) {
$page = intval($request->page);
$posts = Posts::recent(compact('page'));
return new Response(array(
'body' => $render('index', compact('posts'), compact('request'))
));
})
);
/**
* Handles adding new posts.
*/
Router::connect('/add', array(), function($request) use ($render) {
$post = Posts::create();
if (($request->data) && $post->save($request->data)) {
return new Response(array('location' => '/'));
}
return new Response(array(
'body' => $render('edit', compact('post'), compact('request'))
));
});
/**
* Edits existing pages.
*/
Router::connect('/{:slug:[a-z0-9-]+}/edit', array('edit' => true), function($request) use ($render) {
$conditions = array('slug' => $request->slug);
$post = Posts::first(compact('conditions'));
if (($request->data) && $post->save($request->data)) {
return new Response(compact('request') + array('location' => array('slug' => $post->slug)));
}
return new Response(array(
'body' => $render('edit', compact('post'), compact('request'))
));
});
/**
* Handles single page views.
*/
Router::connect('/{:slug:[a-z0-9-]+}', array(), function($request) use ($render) {
$conditions = array('slug' => $request->slug, 'published' => true);
$post = Posts::first(compact('conditions'));
return new Response(array(
'status' => $post ? 200 : 404,
'body' => $render($post ? 'view' : 'error', compact('post'), compact('request'))
));
});
46. class User {
public function somethingUseful() {
Logger::info("Something useful is happening");
// ...
}
}
47. FALE
class User {
public function somethingUseful() {
Logger::info("Something useful is happening");
// ...
} b!!
} Super dum
51. class WebService {
protected $_classes = array(
'socket' => 'lithiumnethttpsocketContext',
'request' => 'lithiumnethttpRequest',
'response' => 'lithiumnethttpResponse'
);
}
54. config/bootstrap/libraries.php
Make conditional
define('LITHIUM_APP_PATH', dirname(dirname(__DIR__)));
define('LITHIUM_LIBRARY_PATH', LITHIUM_APP_PATH . '/../libraries');
require LITHIUM_LIBRARY_PATH . '/lithium/core/Object.php';
require LITHIUM_LIBRARY_PATH . '/lithium/core/StaticObject.php';
Remove when
require LITHIUM_LIBRARY_PATH . '/lithium/util/Collection.php';
require LITHIUM_LIBRARY_PATH . '/lithium/util/collection/Filters.php';
require LITHIUM_LIBRARY_PATH . '/lithium/util/Inflector.php';
require
require
LITHIUM_LIBRARY_PATH
LITHIUM_LIBRARY_PATH
.
.
'/lithium/util/String.php';
'/lithium/core/Adaptable.php';
plugin-izing
require LITHIUM_LIBRARY_PATH . '/lithium/core/Environment.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/Message.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Message.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Media.php';
require LITHIUM_LIBRARY_PATH . '/lithium/net/http/Request.php';
require ....
56. my_awesome_plugin/
config/
bootstrap.php Required
routes.php Optional
anything_in_an_app/
Happens in the filter
we saw in action.php
Unless you
Libraries::add('my_awesome_plugin', array(
'bootstrap' => false disable it
));