Webphp
Leia em 4 minutos
Lightweight framework based on web.py. Only URL handling and action dispatcher was ported.
DOWNLOAD: webphp.zip
VIEW SOURCE: web.php
- See
web.php
source file for changelog. - Docs need to be updated.
API
Request Handling
autodelegate()
- Call the method named REQUEST_METHOD + arg. Example: requesting
/blog/view/
could dispatches the methodGET_view
fromblog
class.
Request
context
(akacxt
)- Object containing some information about the request
basedir
(akahome
)- The base path for the application.
method
- The HTTP method used
fullpath
- The full path requested, including query arguments.
basepath
- The path to root folder. Example: if you access
http://example.com/webphp/blog/view/
,basepath
will be/webphp/blog
. path
- The path after the
basepath
. Example: if you accesshttp://example.com/webphp/blog/view/
,path
will be/blog/view/
. querystring
- The parameters passed on URL, as string. Example:
/blog/view/?id=1&version=print
will returnid=1&version=print
ip
- The client IP address -
$_SERVER['REMOTE_ADDR']
Responses
run($urls)
- Starts handling requests. The mapping
$urls
should be a associative array, where the index is the regular expression and value is the associated class. header($headername, $value)
- Adds the header
headername: value
with the response. output($str)
- Add the specified content to
$web->context->output
attribute, then print it on the page.
HTTP Handlers
By default, these all return simple error messages that send very short messages (like "bad request") to the user. They can and should be overridden to return nicer ones.
expires($seconds)
- Outputs an
Expires
header for$seconds
from now. lastmodified($datetime)
- Outputs a
Last-Modified
header fordatetime
. redirect($url, $status='301 Moved Permanently')
- Returns a
status
redirect to the new URL. If$url
is an array, joins items with base url. found($url)
seeother($url)
tempredirect($url)
- Like
redirect
, but send 302, 303, and 307 headers respectively. badrequest()
- Returns a "400 Bad Request" error. You can overwrite this method by defining a function named
badrequest
. notfound()
- Returns a "404 Not Found" error. You can overwrite this method by defining a function named
notfound
. gone()
- Returns a "410 Gone" error. You can overwrite this method by defining a function named
gone
. internalerror()
- Returns a "500 Internal Server Error". You can overwrite this method by defining a function named
internalerror
. nomethod($allowed=array('GET', 'POST', 'PUT', 'DELETE', 'HEAD'))
- Returns a "405 Method Not Allowed" error. You can overwrite this method by defining a function named
nomethod
.
Template
render($filename, $vars=array(), $base='', $cache_time=0, $flush_cache=false)
$filename
is the name of a file containing the a template in thetemplates
folder or a valid file.$vars
is an associative array used to fill the template.- If
$cache_time
is an integer greater than 0, writes cache to$web->cache_dir
; indicates cache's time duration in seconds (i.e. 3600 means one hour from now) $flush
forces Webphp to write cache content in$web->cache_dir
- If the template is a potential base template (that is, something other templates) can extend, then base should be a string with the name of the template.
set($name, $value)
- Sets a variable
$name
with$value
. fetch($filename, $vars=array())
- Parses and returns the specified
$filename
. $filename
is the name of a file containing the a template in thetemplates
folder or a valid file.$vars
is an associative array used to fill the template.parse($name, $filename, $vars=array())
- Parses the specified
$filename
and sets a variable$name
with the result. $filename
is the name of a file containing the a template in thetemplates
folder or a valid file.$vars
is an associative array used to fill the template.
Attributes
$controller
- The instantiated class.
$offset
- Accessing
/admin/posts/add/
will requireadmin
as your class name. If you set$offset
as1
your required class will bepost
. $plugin_dir
- You can specify a different plugin's folder.
$template_dir
- You can specify a different template's folder.
$cache_dir
- You can specify a different cache's folder.
$cache
- Indicates when Webphp should try to use cached pages.
Other methods
import($plugin[,$plugin2, ..., $plugin3])
- Imports all plugins and instantiates them on
$GLOBALS
. Example:$web->import('cookie', 'form')
importscookie.php
andform.php
debugerror()
- Presents a nice page with lots of debug information for the programmer when a error occurs.
- You can overwrite it by defining a function named
web_debugger
before calling this method. - Based on 500 page from Django (designed by Wilson Miner) and Webpy (coded by Aaron Swartz).
Additional functions
h($str)
- Wrapper for
htmlentities
native function. web_ini()
- If you define a function named
web_ini
, Webphp will execute it before the class method. web_end()
- If you define a function named
web_end
, Webphp will execute it after the class method.
Constants
BASE_URL
- Shortcut for
$web->context->basepath
BASE_DIR
- Shortcut for
$web->context->basedir
CONTROLLER
- The class that is being used by Webphp.
ACTION
- The method that is will be executed. Accessing
/posts/edit/45/
may returnsedit
. METHOD_NAME
- The method's real name. Accessing
/posts/edit/45/
may returnsGET_edit
.
Writing URL
For writing URLs you need to know regular expression. There's no other way. If you don't want or need something complicated you can use something like $url = array('\/contact\/?' => 'contact')
.
Webphp has a new feature over the previous revision. If you want to redirect an url to an specific method, you can use something like $url = array('\/posts\/new\/?' => 'posts::new')
.
Usage
- Create a file named
index.php
. Put the fileweb.php
on the same folder. - Create a file named
.htaccess
with the following code:DirectoryIndex index.php <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !-d RewriteRule (.*) index.php </IfModule> <FilesMatch "web\.php$"> Order allow,deny Deny from all </FilesMatch>
- Create a folder named
templates
. All your templates must be placed there. - Create a file name
hello-world.php
ontemplates
folder. Put the following content:<html> <head> <title>Hello World</title> </head> <body> <h1>Hello World</h1> <p>Hi, <?=$name?>!</p> </body> </html>
- On
index.php
put the following content:<?php require_once '../web.php'; $urls = array( '\/(hello-world\/)?' => 'hello_world' ); class hello_world { function GET() { $name = 'John'; $this->web->render('hello-world.php', get_defined_vars()); } } $web = new web(__FILE__); $web->run($urls); ?>