资源说明:PHP5 framework
Note that `$` signs need to be escaped using `\$` within PHP's [heredoc syntax](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).
#### `label`
`label($forId, $value)`: Returns a HTML `label` element. Eg.:
`)
- `from`: the sending address (either `mail@somewhere.com` or `A Person `)
- `subject`
- `content`
- `attachments`: an associative array, where the key is a filename and the value is the binary data of the attachment
#### Email attachments
Attachments are added to emails like so:
$this->email->send(array(
// ...
'attachments' => array(
'filename.pdf' => 0xDATA,
'filename2.pdf' => 0xDATA
)
));
So in a controller action, a posted file could be added to an email using the `file` helper:
$this->email->send(array(
// ...
'attachments' => array(
$this->data['posted_file']['name'] => $this->file->read_posted_file($this->data['posted_file'])
)
));
#### HTML formatted email
The content type of the email can be set using the `'content_type'` setting. By default the content type is `text/plain`. HTML formatted email can be sent by setting the content type to `text/html`:
$this->email->send(array(
'to' => 'helloworld@swxben.com',
'content' => '
This loops through `$crumbs` which is an associative array where the key is the description for the crumb and the value is the url. If the url is null the crumb is treated as the top.
Views include the breadcrumbs at the top. This would be a widget view (`/widgets/view/15` for example):
partial('/shared/_breadcrumbs', array(
'Home' => $html->url('/'),
'Widgets' => $html->url('/widgets'),
"Viewing widget {$widget['name']}" => null
))); ?>
This would result in something like:
[Home](#) / [Widgets](#) / Viewing widget _sprocket_
## Contribute
If you want to contribute to this project, start by forking the repo. Create an issue if applicable, create a branch in your fork, and create a pull request when it's ready. Thanks!
## License
Licensed under the Attribution-ShareAlike 3.0 Generic ([CC BY-SA 3.0](http://creativecommons.org/licenses/by-sa/3.0/)) license.
Slab includes [BArray](https://github.com/slab-php/barray), an array wrapper which is maintained outside of the Slab project but by the [slab-php](https://github.com/slab-php) organisation.
### Third-party licenses
#### [PHP Markdown & Extra](http://michelf.com/projects/php-markdown)
Copyright (c) 2004-2009 Michel Fortin
All rights reserved.
#### [Original Markdown](http://daringfireball.net/projects/markdown)
Copyright (c) 2004-2006 John Gruber
All rights reserved.
#### [Twitter Bootstrap](http://twitter.github.com/bootstrap/)
Copyright 2012 Twitter, Inc
Licensed under the Apache License v2.0
#### [CakePHP](http://cakephp.org/)
Parts of Slab are based on, inspired by, or originally copied from CakePHP.
Copyright (c) Cake Software Foundation, Inc. ()
MIT License ()
#### [CodeIgniter](http://ellislab.com/codeigniter)
Parts of Slab are based on, inspired by, or originally copied from CodeIgniter.
Copyright (c) 2008 - 2011, EllisLab, Inc.
#### [Lightbox 2](http://lokeshdhakar.com/projects/lightbox2/)
A modified copy of Lightbox 2 is included as a plug-in. It is intended that it will be [pulled out into a standalone plugin](https://github.com/swxben/slab/issues/26).
Licensed under the Creative Commons Attribution 2.5 License -
#### Other third-party licenses
May contain other third-party components under other licenses that I have missed, I will try to keep this up to date.
# Slab ## A PHP5 framework. Slab is/was an MVC framework inspired by CakePHP and CodeIgniter, but I actually want to move away from the strict MVC paradigm and loosen it up. ## Views Views are PHP files that are included in the `View::__render` method. In the scope of the view a number of objects are available: - `$this` is the `View` instance - The `$html` and `$number` helpers - `$pageTitle` is available, and can be set within the view, and used when rendering the layout (eg. ``) - `$dispatcher`, the request-wide `Dispatcher` instance which can be used for rendering partials/subviews: `
- dispatch("/item/view/{$id}", array('data' => $data))->render_to_string()); ?>
Items:
-
dispatch("/my_item/item_view/{$item['id']}")->render_to_string()); ?>
Items:
-
partial("/my_item/item_view/{$item['id']}")); ?>
Link to HELP
### `HtmlHelper - $html` Most of the html methods relate to creating inputs which can be a bit verbose. These are most handy with the `select` statements where the options could come from a database, so it would save a lot of boilerplate code. The most used method is `url()`, which is the recommended method for producing a relative URL to either a controller action or a static file. #### `url` `url($u)`: Wraps `dispatcher->url` which returns either a relative path to a static file or a path to a controller action, optionally using url rewriting for pretty, SEO friendly URLs if enabled (default), optionally including a session ID if the session ID is persisted via the URL. For example, if the site is hosted at `www.domain.com/some/application/`, `url('/pages/home')` may return (depending on the environment): - `/some/application/pages/home` - `/some/application/slab.php?url=/pages/home` - `/some/application/pages/home?session_id=HASH` - `/some/application/slab.php?url=/pages/home&session_id=HASH` If there exists a file at `www.domain.com/some/application/images/header.jpg`, `url('/images/header.jpg')` would return `/some/application/images/header.jpg`. #### `markdown($markdownText)` Passes the [Markdown](http://daringfireball.net/projects/markdown/) formatted input through [Markdown Extra](http://michelf.com/projects/php-markdown/) and returns the resultant HTML. This is really cool within a view: markdown(<<label('data[name]', 'Name:')); ?>
results in: #### `input_hidden` `input_hidden($params)`: returns a hidden input element. Params is an array containing optionally `name`, `id` and `value`. Eg: results in: #### `input`, `input_text`, `input_url`, `input_file` `input($params)` also `input_text` `input_url` and `input_file`: returns an input with an optional label. Params is an array containing optionally `name`, `id`, `value`, `label`, `type`. If `label` is not included or is null, no label will be output. The `input_text`, `input_url` and `input_file` methods include the `type` value. Eg: results in: #### `textarea` `textarea($params)`: returns a `textarea`. Params is an array containing optionally `name`, `id`, `value`, `label`, `rows` (default to 8) and `cols` (default to 80). #### `select` `select($params)`: returns a `select` element. Params is an array containing optionally `name`, `id`, `options`, `current` and `label`. Eg: 'Australia', 'NZ' => 'New Zealand', 'US' => 'United States', 'GB' => 'Great Britain' ); ?>locations(array( 'name' => 'data[location]', 'id' => 'location', 'options' => $locations, 'current' => $user['current_location'], 'label' => 'Select your location' ))); ?>
results in (reformatted):#### `select_int_from_range` `select_int_from_range($name, $id, $from, $to, $current)`: eg.: select_int_from_range('age', 'age', 0, 100, 32)); ?> results in (reformatted): #### `header_status` `header_status($code, $reason = null)`: Sets the HTTP header status. If the reason is not provided it uses a lookup table for standard HTTP status codes. Eg: `$html->header_status(501);` may result in `header('HTTP/1.1 501 Not Implemented');`. #### `header_no_cache` `header_no_cache()`: Writes the headers required to trigger `no-cache` for Internet Explorer. ### NumberHelper - $number #### currency #### format ## Components Components are used in controllers. There are several built-in controllers, all of which subclass `Component`. Each component has an `init()` method that is called before the component's `before_action()` and `before_filter()` methods. Then after the action method is dispatched and the controller's `after_action()` and `after_filter()` methods are called, the component's `after_action()` anf `after_filter()` methods are called, followed by the component's `shutdown()` method. `before_action()` and `before_filter()` should be considered synonyms for convenience and probably shouldn't both be implemented in the same component. Likewise `after_action()` and `after_filter()` should be considered convenience synonyms. Components are available in each controller's class-wide scope, so within a controller action method: function some_action() { $this->file->write('/temp/foo.txt', 'some text'); } ### Cookie ### Db ### Email The email component makes sending a formatted email relatively simple. The originating view will contain a form: /app/views/contact/index.php: ... The `/contact/submit` action does the work of getting the content (by way of a partial view) and sending the email: /app/controllers/contact_controller.php: ... function submit() { $this->email->send(array( 'to' => 'helloworld@swxben.com', 'subject' => 'Contact form', 'content' => $this->dispatcher->partial('/contact/_submit_email', array( 'contactEmail' => $this->data['contact_email'] )) )); } The partial view converts the input into a formatted email: /app/controllers/contact_controller.php: ... function _submit_email() { $this->set($this->data); } /app/views/contact/_submit_email.php: ... Email address: ... The available settings that can be passed to `email->send` are: - `to`: the recipient address (either `mail@somewhere.com` or `A Person
HTML email
', 'content_type' => 'text/html' )); ### File #### `exists($filename)` #### `is_posted_file` Returns whether the POSTed value is a posted file. A view might post to an action: Then in the action: class FilesController extends AppController { function upload() { if ($this->file->is_uploaded_file($this->data['file'])) { $this->text('A file was uploaded'); } else { $this->text('No file was uploaded'); } } } #### `load_posted_file` / `read_posted_file` Reads a posted file to a byte buffer. A view might post to an action: Then in the action: class FilesController extends AppController { function upload() { $filename = $this->data['file']['name']; $fileData = $this->file->read_posted_file($this->data['file']); $this->filesTable->save(array( 'filename' => $filename, 'data' => $fileData )); } } #### `read` / `read_text` #### `write` / `write_text` #### `write_object($filename, $data, $mode = 'wb', $useEncryption = true)` #### `read_object($filename, $useEncryption = true)` #### `remove($filename)` / `delete($filename)` Wrappers for `unlink()`. #### `dir($path, $filesOnly = false)` Get a directory listing of the path. This includes any subdirectories and returns the full path to each entry. #### `store_file($sourcePath, $destinationKey)` Copies the specified file (`$postedFile['tmp_name']` is typical) to `/app/stored_files/{hashed $destinationKey}`. Files can then be read using `read_stored_file($destinationKey)`, so just the key has to be stored in the database. #### `read_stored_file($destinationKey)` Returns the contents of a stored file (located at `/app/stored_files/{hashed $destinationKey}`). Use this in a controller like so: function get_file($id) { $id = (int)$id; $data = $this->someModel->get($id); $storedFileData = $this->file->read_stored_file($data['destination_key']); $this->file_attachment($data['filename'], $storedFileData); } #### `rename($source, $dest)` Wraps `rename()` which moves the file `$source` to `$dest`. #### `copy($source, $dest)` Wraps `copy()` which copies the file `$source` to `$dest`. ### Image #### `load_image_from_file($filename)` #### `get_posted_image($postImg)` #### `resize_image($source, $maxWidth, $maxHeight)` #### `resize_image_horizontal($source, $newWidth)` #### `resize_with_background($source, $width, $height, $hexRGB)` #### `save_image_to_in_memory_jpeg($img)` ### Session ## Dispatcher The dispatcher is part of the lifecycle of a page request. The dispatcher is available: - in views in the local scope, as `$dispatcher` - in controllers in the class scope, as `$this->dispatcher` ### Slugs Slugs are a way of identifying a page throughout the request. The slug can be set in a view or controller action: - in a view: `set_slug('contact'); ?>` - in the action: `function contact() { $this->dispatcher->set_slug('contact'); }` Then the slug can be used to control the layout via the `slug_is` function which returns a boolean: The `slug_is` function can optionally take an array where if the slug is in the array it will return true: $dispatcher->slug_is(array('home, contact')) The slug can also be retrieved by calling `$dispatcher->get_slug()`. ## Global functions Global functions are defined in `global_functions.php` and are mostly shortcuts for echo and escape operations. Some of the functions are based on similar functions in CakePHP and CodeIgniter. ### `any($val)` Returns true if the value is not null and has a count of more than 0. Kind of the reverse of `empty()` except that `!empty(1)` is true whereas `any(1)` is false. ### `e($s)` ### `eh($)` ### `env($key)` ### `get_mime_type($ext)` ### `get_mime_type_from_filename($filename)` ### `get_microtime()` ### `h($s)` / `html($s)` ### `hex2rgb($hex)` ### `length($a)` Alias for `count($a)` ### `lowercase($s)`, `toLower($s)`, `lc($s)`, `low($s)` ### `pr($s)` ### `str_contains($s)` ### `str_starts_with($s)` ### `uppercase($s)`, `toUpper($s)`, `uc($s)`, `up($s)` ### `uuid_secure()` ## Plugins Plugins are sets of controllers, views, etc., that are bundled together under `/app/plugins`. There are some built-in plugins that are part of Slab - the main one being the `SlabInterals` controller which is used for 'pretty' error screens. The dispatcher resolves a controller in the following order: 1. Within the application's controllers 1. Within the application's plugins 1. Within the built-in Slab plugins So the dispatcher would attempt a request to `/foo/bar/1` via: 1. `app/controllers/foo_controller.php` -> `FooController::bar(1)` 1. `app/plugins/foo/controllers/foo_controller.php` -> `FooController::bar(1)` 1. `lib/plugins/foo/controllers/foo_controller.php` -> `FooController::bar(1)` See the Bootstrap example application for an example plugin that sets up a [Twitter Bootstrap Carousel component](http://twitter.github.com/bootstrap/javascript.html#carousel). ### Official plugins - [slab-plugin-lightbox2](https://github.com/swxben/slab-plugin-lightbox2) - based on [Lightbox 2](http://lokeshdhakar.com/projects/lightbox2/) ## PageLogger `PageLogger` is a very simple way of logging events throughout a page request. A page logger instance is created in the application's `slab.php` script, the `'slab_init'` event is logged, and the page logger is passed to the dispatcher: $pageLogger->log('slab_init'); This records the event at the current time (`get_microtime()`, in `global_functions.php`). The page logger is then passed to the dispatcher where it can be accessed at `$dispatcher->pageLogger`. Events can be logged at any point. Calling `$pageLogger->to_table()` returns the log as a HTML table. This could happen at the end of the site template if debugging is enabled (not currently part of Slab itself). ## Examples ### Bootstrap Breadcrumbs [Twitter Bootstrap](http://twitter.github.com/bootstrap/) includes a [breadcumb component](http://twitter.github.com/bootstrap/components.html#breadcrumbs). Slab partials can be used to create reusable breadcrumbs. Start with a `_breadcrumbs` view in a `SharedController` - `controllers/shared_controller.php`: class SharedController extends AppController { function _breadcrumbs() { $this->set('crumbs', $this->data['crumbs']); } } This action receives crumbs via the action data which will be shown later. The `views/shared/_breadcrumbs.php` view generates the markup for the breadcrumbs:
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。