Improved URL rewriting support
When this code was written, it did not include an internal URL mapper and each page was loaded by a PHP script. The internal URL was a recent, mostly unfinished addition. Base URL is now supported: * for views, when they implement the BaseURLAware interface (a base class that does what most views will do with that is provided - BaseURLAwareView), * in the menu, * in form actions, * in boxes (for buttons, and for the contents if the inner view implements BaseURLAware).
This commit is contained in:
parent
3b91a6fc8c
commit
29a026e71a
5 changed files with 69 additions and 10 deletions
|
@ -32,11 +32,18 @@ class BoxButton
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render( )
|
public function render( $baseURL )
|
||||||
{
|
{
|
||||||
|
$url = $this->URL;
|
||||||
|
if ( $url{0} != ':' ) {
|
||||||
|
if ( $url{0} != '/' ) {
|
||||||
|
$url = "/$url";
|
||||||
|
}
|
||||||
|
$url = $baseURL . $url;
|
||||||
|
}
|
||||||
return HTML::make( 'a' )
|
return HTML::make( 'a' )
|
||||||
->setAttribute( 'title' , HTML::from( $this->title ) )
|
->setAttribute( 'title' , HTML::from( $this->title ) )
|
||||||
->setAttribute( 'href' , $this->URL )
|
->setAttribute( 'href' , $url )
|
||||||
->setAttribute( 'class' ,
|
->setAttribute( 'class' ,
|
||||||
'box-button' . ( ( $this->class === null )
|
'box-button' . ( ( $this->class === null )
|
||||||
? '' : ( ' ' . $this->class ) ) )
|
? '' : ( ' ' . $this->class ) ) )
|
||||||
|
@ -54,8 +61,8 @@ class BoxButton
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
final class View_Box
|
class View_Box
|
||||||
implements View
|
extends BaseURLAwareView
|
||||||
{
|
{
|
||||||
protected $title;
|
protected $title;
|
||||||
protected $class;
|
protected $class;
|
||||||
|
@ -72,6 +79,15 @@ final class View_Box
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function setBaseURL( $baseURL )
|
||||||
|
{
|
||||||
|
parent::setBaseURL( $baseURL );
|
||||||
|
if ( $this->contents instanceof BaseURLAware ) {
|
||||||
|
$this->contents->setBaseURL( $baseURL );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function setClass( $class )
|
public function setClass( $class )
|
||||||
{
|
{
|
||||||
$this->class = $class;
|
$this->class = $class;
|
||||||
|
@ -110,7 +126,7 @@ final class View_Box
|
||||||
$buttons = HTML::make( 'div' )
|
$buttons = HTML::make( 'div' )
|
||||||
->setAttribute( 'class' , 'box-buttons' );
|
->setAttribute( 'class' , 'box-buttons' );
|
||||||
foreach ( $this->buttons as $button ) {
|
foreach ( $this->buttons as $button ) {
|
||||||
$buttons->appendElement( $button->render( ) );
|
$buttons->appendElement( $button->render( $this->base ) );
|
||||||
}
|
}
|
||||||
$box->appendElement( $buttons );
|
$box->appendElement( $buttons );
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,9 @@ abstract class Page
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ( ! is_null( $rc ) ) {
|
} elseif ( ! is_null( $rc ) ) {
|
||||||
|
if ( $rc{0} != '/' ) {
|
||||||
|
$rc = $this->baseURL . '/' . $rc;
|
||||||
|
}
|
||||||
header( "Location: $rc" );
|
header( "Location: $rc" );
|
||||||
$rv = true;
|
$rv = true;
|
||||||
}
|
}
|
||||||
|
@ -121,7 +124,7 @@ abstract class HTMLPage
|
||||||
foreach ( $menu as $link => $title ) {
|
foreach ( $menu as $link => $title ) {
|
||||||
$html->appendElement( HTML::make( 'li' )
|
$html->appendElement( HTML::make( 'li' )
|
||||||
->appendElement( HTML::make( 'a' )
|
->appendElement( HTML::make( 'a' )
|
||||||
->setAttribute( 'href' , $link )
|
->setAttribute( 'href' , $this->getBaseURL() . '/' . $link )
|
||||||
->setAttribute( 'title' , HTML::from( $title ) )
|
->setAttribute( 'title' , HTML::from( $title ) )
|
||||||
->appendText( $title ) ) );
|
->appendText( $title ) ) );
|
||||||
}
|
}
|
||||||
|
@ -170,6 +173,9 @@ abstract class HTMLPage
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( $this->views as $view ) {
|
foreach ( $this->views as $view ) {
|
||||||
|
if ( $view instanceof BaseURLAware ) {
|
||||||
|
$view->setBaseURL( $this->getBaseURL( ) );
|
||||||
|
}
|
||||||
$container->append( $view->render( ) );
|
$container->append( $view->render( ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,29 @@ interface View
|
||||||
public function render( );
|
public function render( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface BaseURLAware
|
||||||
|
{
|
||||||
|
public function setBaseURL( $baseURL );
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class BaseURLAwareView
|
||||||
|
implements View , BaseURLAware
|
||||||
|
{
|
||||||
|
protected $base;
|
||||||
|
|
||||||
|
public function setBaseURL( $baseURL )
|
||||||
|
{
|
||||||
|
$this->base = $baseURL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TitleProvider
|
||||||
|
{
|
||||||
|
|
||||||
|
public function getTitle( );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
final class HTML
|
final class HTML
|
||||||
{
|
{
|
||||||
|
|
|
@ -65,9 +65,15 @@ class Ctrl_Form
|
||||||
return $this->form->view( );
|
return $this->form->view( );
|
||||||
}
|
}
|
||||||
if ( $cResult ) {
|
if ( $cResult ) {
|
||||||
return $this->form->successURL( );
|
$url = $this->form->successURL( );
|
||||||
|
} else {
|
||||||
|
$url = $this->form->cancelURL( );
|
||||||
}
|
}
|
||||||
return $this->form->cancelURL( );
|
|
||||||
|
if ( $url{0} != '/' ) {
|
||||||
|
$url = "/$url";
|
||||||
|
}
|
||||||
|
return $page->getBaseURL( ) . $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class View_Form
|
class View_Form
|
||||||
implements View
|
extends BaseURLAwareView
|
||||||
{
|
{
|
||||||
protected $form;
|
protected $form;
|
||||||
|
|
||||||
|
@ -161,10 +161,18 @@ class View_Form
|
||||||
$name = $this->form->name();
|
$name = $this->form->name();
|
||||||
$prefix = $name . '-';
|
$prefix = $name . '-';
|
||||||
|
|
||||||
|
$action = $this->form->action( );
|
||||||
|
if ( $action{0} != '?' ) {
|
||||||
|
if ( $action{0} != '/' ) {
|
||||||
|
$action = "/$action";
|
||||||
|
}
|
||||||
|
$action = $this->base . $action;
|
||||||
|
}
|
||||||
|
|
||||||
$form = HTML::make( 'form' )
|
$form = HTML::make( 'form' )
|
||||||
->setAttribute( 'name' , $name )
|
->setAttribute( 'name' , $name )
|
||||||
->setAttribute( 'id' , $prefix . 'form' )
|
->setAttribute( 'id' , $prefix . 'form' )
|
||||||
->setAttribute( 'action' , $this->form->action( ) )
|
->setAttribute( 'action' , $action )
|
||||||
->setAttribute( 'method' , $this->form->method( ) )
|
->setAttribute( 'method' , $this->form->method( ) )
|
||||||
->append( $this->renderHiddenFields( $prefix ) )
|
->append( $this->renderHiddenFields( $prefix ) )
|
||||||
->append( $visibleArea = HTML::make( 'dl' ) );
|
->append( $visibleArea = HTML::make( 'dl' ) );
|
||||||
|
|
Loading…
Reference in a new issue