svn: /pear/peardoc/trunk/en/package/networking/net-url-mapper/ example.xml

[email protected] (Till Klampaeckel)
Newsgroups php.pear.doc
Message-ID <[email protected]>
till                                     Sun, 04 Oct 2009 15:10:47 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=289186

Log:
added more examples

Changed paths:
    U   pear/peardoc/trunk/en/package/networking/net-url-mapper/example.xml

Modified: pear/peardoc/trunk/en/package/networking/net-url-mapper/example.xml
===================================================================
--- pear/peardoc/trunk/en/package/networking/net-url-mapper/example.xml	2009-10-04 14:56:29 UTC (rev 289185)
+++ pear/peardoc/trunk/en/package/networking/net-url-mapper/example.xml	2009-10-04 15:10:47 UTC (rev 289186)
@@ -1,13 +1,151 @@
 <?xml version="1.0" encoding="utf-8"?>
 <chapter xmlns="http://docbook.org/ns/docbook" version="lillet"
  xml:id="package.networking.net-url-mapper.example"
+ xmlns:phd="http://www.php.net/ns/phd"
 >
  <info>
-  <title>Examples for Net_URL_Mapper</title>
+  <title>Net_URL_Mapper Examples</title>
  </info>

  <para>
-EXAMPLEZ
+  The following examples assume/need the following setup:
  </para>

+ <example>
+  <info><title>.htaccess</title></info>
+  <programlisting><![CDATA[
+DirectoryIndex router.php
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteRule !\.(js|ico|gif|jpg|png|css)$ /router.php
+  ]]></programlisting>
+ </example>
+
+ <example>
+  <info><title>router.php</title></info>
+  <programlisting><![CDATA[
+<?php
+require_once 'Net/URL/Mapper.php';
+$m = Net_URL_Mapper::getInstance();
+  ]]></programlisting>
+ </example>
+
+ <phd:toc phd:element="package.networking.net-url-mapper.example"/>
+
+ <para>
+  The following examples use <literal>.htaccess</literal> and <literal>router.php</literal> as a base.
+ </para>
+
+ <section
+  xml:id="package.networking.net-url-mapper.example.blog"
+  phd:chunk="false"
+ >
+
+  <info><title>Blog routing</title></info>
+
+  <para>
+   The following snippets maps a couple blog URLs.
+  </para>
+
+  <example>
+   <info><title>blog.php</title></info>
+   <programlisting role="php">
+     <![CDATA[
+require 'router.php';
+
+$m->connect('blog', array('page' => 'frontpage.php'));
+$m->connect('blog/feed/:type', array('page' => 'feed.php', 'type' => 'rss'));
+$m->connect('blog/archives/:year/:month', array('page' => 'archive.php', 'year' => date('Y'), 'month' => date('m'));
+
+$route = $m->match($_SERVER['REQUEST_URI']);
+if ($route === null) { // no match
+    $route = array(
+        'page' => 'frontpage.php',
+    );
+}
+include dirname(__FILE__) . '/inc/' . $route['page'];
+     ]]>
+   </programlisting>
+  </example>
+ </section>
+
+ <section
+  xml:id="package.networking.net-url-mapper.example.zf"
+  phd:chunk="false"
+ >
+  <info><title>Zend Framework routing</title></info>
+
+  <para>
+   A really simple example for Zend Framework-style URL routing. ;-) This is not
+   meant to work in production, it's not necessarily secure and serves as a proof
+   of concept or base for a real implementation.
+  </para>
+
+  <example>
+   <info><title>zf.php</title></info>
+   <programlisting role="php">
+     <![CDATA[
+require 'router.php';
+
+$path     = '/:module/:controller/:action';
+$defaults = array(
+    'module'     => 'default',
+    'controller' => 'index',
+    'action'     => 'index',
+);
+
+$m->connect($path, $defaults);
+
+$route = $m->match($_SERVER['REQUEST_URI']);
+
+$controllerClass  = ucfirst(strtolower($route['controller'])) . 'Controller';
+$controllerAction = strtolower($route['action']) . 'Action';
+
+require 'app/modules/' . $route['module'] . '/' . $class . '.php';
+
+$controllerObj = new $class;
+call_user_func(array($controllerObj, $controllerAction);
+     ]]>
+   </programlisting>
+  </example>
+ </section>
+
+ <section
+  xml:id="package.networking.net-url-mapper.example.generate"
+  phd:chunk="false"
+ >
+
+  <info><title>Generate a URL</title></info>
+
+  <para>
+   The following snippets explains how to generate a fancy URL.
+  </para>
+
+  <example>
+   <info><title>blog.generate.php</title></info>
+   <programlisting role="php">
+     <![CDATA[
+require 'router.php';
+
+$m->connect('blog', array('page' => 'frontpage.php'));
+$m->connect('blog/feed/:type', array('page' => 'feed.php', 'type' => 'rss'));
+$m->connect('blog/archives/:year/:month', array('page' => 'archive.php', 'year' => date('Y'), 'month' => date('m'));
+
+$m->generate(array(
+    'page' => 'feed.php',
+    'type' => 'atom',
+)); // blog/feed/atom
+
+$m->generate(array(
+    'page'  => 'archive.php',
+    'year'  => '2008',
+    'month' => '06',
+)); // blog/archives/2008/06
+
+     ]]>
+   </programlisting>
+  </example>
+ </section>
+
 </chapter>
\ No newline at end of file
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.