So I'm writing a framework on which I want to base a few apps that I'm working on (the framework is there so I have an environment to work with, and a system that will let me, for example, use a single sign-on)
I want to make this framework, and the apps it has use a Resource Oriented Architecture.
Now, I want to create a URL routing class that is expandable by APP writers (and possibly also by CMS App users, but that's WAYYYY ahead in the future) and I'm trying to figure out the best way to do it by looking at how other apps do it.
I prefer to use reg ex over making my own format since it is common knowledge. I wrote a small class that I use which allows me to nest these reg ex routing tables. I use to use something similar that was implemented by inheritance but it didn't need inheritance so I rewrote it.
I do a reg ex on a key and map to my own control string. Take the below example. I visit /api/related/joe and my router class creates a new object ApiController and calls it's method relatedDocuments(array('tags' => 'joe'));
// the 12 strips the subdirectory my app is running in
$index = urldecode(substr($_SERVER["REQUEST_URI"], 12));
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)/post$#Di" => "ThreadController/post/title",
"#^thread/(.*)/reply$#Di" => "ThreadController/reply/title",
"#^thread/(.*)$#Di" => "ThreadController/thread/title",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
In order to keep errors down and simplicity up you can subdivide your table. This way you can put the routing table into the class that it controls. Taking the above example you can combine the three thread calls into a single one.
Route::process($index, array(
"#^api/related/(.*)$#Di" => "ApiController/relatedDocuments/tags",
"#^thread/(.*)$#Di" => "ThreadController/route/uri",
"#^ajax/tag/(.*)/(.*)$#Di" => "TagController/add/id/tags",
"#^ajax/reply/(.*)/post$#Di"=> "ThreadController/ajaxPost/id",
"#^ajax/reply/(.*)$#Di" => "ArticleController/newReply/id",
"#^ajax/toggle/(.*)$#Di" => "ApiController/toggle/toggle",
"#^$#Di" => "HomeController",
));
Then you define ThreadController::route to be like this.
function route($args) {
Route::process($args['uri'], array(
"#^(.*)/post$#Di" => "ThreadController/post/title",
"#^(.*)/reply$#Di" => "ThreadController/reply/title",
"#^(.*)$#Di" => "ThreadController/thread/title",
));
}
Also you can define whatever defaults you want for your routing string on the right. Just don't forget to document them or you will confuse people. I'm currently calling index if you don't include a function name on the right. Here is my current code. You may want to change it to handle errors how you like and or default actions.
Yet another framework? -- anyway...
The trick is with routing is to pass it all over to your routing controller.
You'd probably want to use something similar to what I've documented here:
http://www.hm2k.com/posts/friendly-urls
The second solution allows you to use URLs similar to Zend Framework.
Use a list of Regexs to match which object I should be using
For example
^/users/[\w-]+/bookmarks/(.+)/$
^/users/[\w-]+/bookmarks/$
^/users/[\w-]+/$
Pros: Nice and simple, lets me define routes directly
Cons: Would have to be ordered, not making it easy to add new things in (very error prone)
This is, afaik, how Django does it
I think a lot of frameworks use a combination of Apache's mod_rewrite and a front controller. With mod_rewrite, you can turn a URL like this: /people/get/3 into this:
index.php?controller=people&method=get&id=3. Index.php would implement your front controller which routes the page request based on the parameters given.
As you might expect, there are a lot of ways to do it.
For example, in Slim Framework , an example of the routing engine may be the folllowing (based on the pattern ${OBJECT}->${REQUEST METHOD}(${PATTERM}, ${CALLBACK}) ):
$app->get("/Home", function() {
print('Welcome to the home page');
}
$app->get('/Profile/:memberName', function($memberName) {
print( 'I\'m viewing ' . $memberName . '\'s profile.' );
}
$app->post('/ContactUs', function() {
print( 'This action will be fired only if a POST request will occure');
}
So, the initialized instance ($app) gets a method per request method (e.g. get, post, put, delete etc.) and gets a route as the first parameter and callback as the second.
The route can get tokens - which is "variable" that will change at runtime based on some data (such as member name, article id, organization location name or whatever - you know, just like in every routing controller).
Personally, I do like this way but I don't think it will be flexible enough for an advanced framework.
Since I'm working currently with ZF and Yii, I do have an example of a router I've created as part of a framework to a company I'm working for:
The route engine is based on regex (similar to #gradbot's one) but got a two-way conversation, so if a client of yours can't run mod_rewrite (in Apache) or add rewrite rules on his or her server, he or she can still use the traditional URLs with query string.
The file contains an array, each of it, each item is similar to this example:
$_FURLTEMPLATES['login'] = array(
'i' => array( // Input - how the router parse an incomming path into query string params
'pattern' => '#Members/Login/?#i',
'matches' => array( 'Application' => 'Members', 'Module' => 'Login' ),
),
'o' => array( // Output - how the router parse a query string into a route
'#Application=Members(&|&)Module=Login/?#' => 'Members/Login/'
)
);
You can also use more complex combinations, such as:
$_FURLTEMPLATES['article'] = array(
'i' => array(
'pattern' => '#CMS/Articles/([\d]+)/?#i',
'matches' => array( 'Application' => "CMS",
'Module' => 'Articles',
'Sector' => 'showArticle',
'ArticleID' => '$1' ),
),
'o' => array(
'#Application=CMS(&|&)Module=Articles(&|&)Sector=showArticle(&|&)ArticleID=([\d]+)#' => 'CMS/Articles/$4'
)
);
The bottom line, as I think, is that the possibilities are endless, it just depend on how complex you wish your framework to be and what you wish to do with it.
If it is, for example, just intended to be a web service or simple website wrapper - just go with Slim framework's style of writing - very easy and good-looking code.
However, if you wish to develop complex sites using it, I think regex is the solution.
Good luck! :)
You should check out Pux https://github.com/c9s/Pux
Here is the synopsis
<?php
require 'vendor/autoload.php'; // use PCRE patterns you need Pux\PatternCompiler class.
use Pux\Executor;
class ProductController {
public function listAction() {
return 'product list';
}
public function itemAction($id) {
return "product $id";
}
}
$mux = new Pux\Mux;
$mux->any('/product', ['ProductController','listAction']);
$mux->get('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->post('/product/:id', ['ProductController','updateAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->delete('/product/:id', ['ProductController','deleteAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$route = $mux->dispatch('/product/1');
Executor::execute($route);
Zend's MVC framework by default uses a structure like
/router/controller/action/key1/value1/key2/value2
where router is the router file (mapped via mod_rewrite, controller is from a controller action handler which is defined by a class that derives from Zend_Controller_Action and action references a method in the controller, named actionAction. The key/value pairs can go in any order and are available to the action method as an associative array.
I've used something similar in the past in my own code, and so far it's worked fairly well.
Try taking look at MVC pattern.
Zend Framework uses it for example, but also CakePHP, CodeIgniter, ...
Me personally don't like the MVC model, but it's most of the time implemented as "View for web" component.
The decision pretty much depends on preference...
Related
How to enable multitranslation in yii 2 basic framework? I have tried, but it is not working. I get no error, but translation is not showing. Code:
public function actionLang(){
$lang = \Yii::$app->request->get('lang');
if($lang && in_array($lang,['en-US','ar-SA'])){
$cookie = new Cookie();
$cookie->name = '_lang';
$cookie->value = $lang;
$cookie->expire = time() + 60 * 60 * 24 * 180;
\Yii::$app->response->cookies->add($cookie);
}
$this->redirect(['index']);
}
I'm using this function in SiteController.
Internationalization in Yii is not a one-action job. Here's the documentation on how to make your website multilingual:
https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-i18n.md
If docs are unclear, here is a tutorial:
http://code.tutsplus.com/tutorials/programming-with-yii2-localization-with-i18n--cms-23140
If you have gone through all the steps and merely wish to set the current language, you can use:
\Yii::$app->language = 'xxx';
where xxx is a language code in accordance with ISO 639-2.
Here are the mentioned standard's entries:
http://www.loc.gov/standards/iso639-2/php/code_list.php
First of all, from what I have gathered, you are trying to identify a language requested from the current request by doing $lang = \Yii::$app->request->get('lang'); and then set it in the cookie. In my opinion, this should be used as a "helper", meaning, it is useful to know the language preference of the returning client, but you still have to manage languages via URL, i.e. http://yoursite.com/en or http://yoursite.com/de should serve different languages.
Now, there are actually very good plugins out there for multilingual URL management, such as yii2-localeurls , which you can find here. I use it in production in multiple projects and highly recommend it.
To actually manage translations, you have to have a folder in the root of your project (if you are using advance template, you should have it inside frontend/backend/console ) to store the actual block translations, call it messages .
Now, under #app/messages create folders for each non-default language, for example #app/messages/de for German translations.
If you go your config in #app/config/main.php, look for i18n key inside the components array. If you can't find such a key, simply put the following into the components array:
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en',
'fileMap' => [
'app' => 'app.php',
'app/error' => 'error.php',
],
],
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
]
],
],
Now, you will need to create a translation file inside the relevant directory. In the above configuration, we declared that the default language used is en, that means that all original messages would be in English. Go to #app/messages/de and create a translations file inside that directory. You can call it whatever you like, but for this example, call it site.php.
In this site.php put the following:
return [
'Translate this!' => 'Your relevant translation here in whichever language',
'Translate this also!!!' => 'Stuff...'
];
If all done correctly, when you access your page via http://yousite.com/de, when using Yii::t('site', 'Translate this!') you should be getting 'Your relevant translation here in whichever language' instead.
The beauty of Yii 2 is that it is extremely well documented. Visit the official documentation if you are stuck, it really explains everything quite well.
I am creating my own MVC framework in php as a means to learn more advanced programming. I've got the framework up and running however I have an issue regarding the current routing method. I want the framework to support a backend cms to compliment the front end website. The issue is that my routing structure works like so - mywebsite.com/controller/method/id
The routing engine sorts the information into an array like this
segments 0 => controller, 1 => method, 2 => id
Right now if I visit mywebsite.com/projects it takes me to what I have set up as an admin page. Not only do I want this to be accessible only from mywebsite.com/admin/projects, I want the mywebsite.com/projects to take me to frontend.
So if I want to visit mywebsite.com/projects I'd like it to render the "front" controller, pushing "projects" into the method. If I visit mywebsite.com/admin/projects I'd like it to load the projects controller.
Here's the current routing class in whole as follows.
<?php
class Request {
//url domain.com/controller/method/other
//holds url segments 0 => controller, 1 => method, 2 => other, etc
public $segments;
function __construct() {
$this->parse_globals();
}
function parse_globals(){
//$uri = preg_replace("|/(.*)|", "\\1", str_replace("\\", "/", $_SERVER['REQUEST_URI']));
$uri = (empty($_GET['rt'])) ? '' : $_GET['rt'];
$this->segments = array_filter(explode('/', $uri));
if (in_array("admin", $this->segments)) {
array_shift($this->segments);
}
print_r($this->segments);
//remove index php
if( reset($this->segments) == 'index.php') {
array_shift ($this->segments);
}
}
function controller(){
return $this->segment(0);
}
function method(){
return $this->segment(1);
}
function param( $str ){
return isset($_REQUEST[$str]) ? $_REQUEST[$str] : false;
}
function segment( $no ){
return isset($this->segments[$no]) ? $this->segments[$no] : false;
}
}
Instead of simply using explode() to separate the segments of URL, you could use a set of regular expression pattern.
For example, this following patter would try to match at firsts action, and, if action exists them check if there is controller set before it:
'/(:?(:?\/(?P<controller>[^\/\.,;?\n]+))?\/(?P<action>[^\/\.,;?\n]+))?/'
Most of PHP frameworks use different ways to generate such patterns, with simplified notations. This way you can set which parts for each pattern are mandatory and which optional. And it is also possible to provide fallback values for the optional parts.
That said ...
Before starting to make something so complicated as cms with framework, you might invest some additional time in researching OOP. I would recommend to at least watch lectures from Miško Hevery and Robert C. Martin. Just because you think, that you know how to write a class, does not mean, that you understands object oriented programming.
Update
I have listed few materials in this answer. You might find them useful,
Additionally here are two more lectures, that were not in answer above:
Clean Code I: Arguments
Clean Code III: Functions
My understanding is that there's three routing cases:
The basic one:
/<controller>/<action>/<parameters>
A special one for the admin panel (where "admin" would be a kind of separate module):
/<module>/<controller>/<action>/<parameters>
And finally a special case for "/projects" which maps to "/front/projects".
In that case, you need to make your routing class more flexible so that it can handle any kind of routing scheme. In a framework like Kohana, this would be done with rules such as:
Route::set('adminModule', 'admin/projects')
->defaults(array(
'controller' => 'projects',
'action' => 'admin',
));
Route::set('projectPage', 'projects')
->defaults(array(
'controller' => 'front',
'action' => 'projects',
));
Route::set('default', '(<controller>(/<id>(/<action>)))')
->defaults(array(
'controller' => 'index',
'action' => 'index',
));
Obviously this is just an example but you get the idea. Basically, you want to provide some sensible default routing (eg. controller/action/id) but you also need to allow users to configure other routes.
I am currently developing a php router which is targeted at extreme high performance. you probably might want to take a look.
https://github.com/c9s/Pux
We also provide a pattern compiler that is doing the same thing as yours. but you can write a simpler path instead of complex patterns.
e.g., you may write something like this:
/product/:id/:name [ id => '\d+', name => '\w+' ]
FYI:
Pux is 48.5x faster than symfony router in static route dispatching, 31x faster in regular expression dispatching. (with pux extension installed)
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.
I've been tasked with rewriting an existing website with large pre-existing link catalog. For argument's sake, let's assume we can't do anything that would change the link catalog. Here's a few examples of the link structure we're working with:
An item page would be:
www.domain.com/widgets/some-totally-awesome-large-purple-widget
A category sub page page would be:
www.domain.com/widgets/purple-widgets
A category parent page page would be:
www.domain.com/widgets/
A custom page may be:
www.domain.com/some-random-page
The various page types are too numerous to write individual Routers for.
Using Router::connect I can easily account for the first and second scenarios using something like:
Router::connect('/{:pageroot}/{:pagekey}', 'Pages::index');
In turn, the Pages::index method looks for entries in our database with the "key" of '/widgets/purple-widgets'.
However, the framework defaults to the '/{:controller}/{:action}/{:args}' route for pages like the third and fourth. I know that this is the correct behavior for the framework. Also, best practice would state that I should write the site to match this behavior. But, that isn't an option here.
What I need is a Router that would allow the third and fourth examples to function the same as the first. All examples should be sent to the Pages::index controller, which in turn queries a database using the URL path as a key.
If you don't have any convention in the URL for what is what, between page, item and category. I'd go with a very generic router.
Router::connect('/{:category}/{:page}/{:item}', 'Pages::any');
Router::connect('/{:category}/{:page}', array('Pages::any', 'item' => null));
Router::connect('/{:category}', array('Pages::any', 'page' => null, 'item' => null));
And in Pages::any() to search for the correct stuff. Is that category a page after all (example 4)? Is that page an item (example 1)?
or
You store the URL somewhere (e.g. a mapping table in the database) and use the pattern version of a lithium Route.
Router::connect(new Route(array(
'pattern' => '#^/(?<path>.+)$#',
'params' => array('controller' => 'pages', 'action' => 'any'),
'keys' => array('path' => 'path'),
// extra stuff, if the path is `tata`, it skips this route and uses
// any of the following ones that matches.
'handler' => function($request) {
if ($request->params['path'] == 'tata') {
return false;
} else {
return $request;
}
}
)));
From that point, you'll get the full URL.
You probably should write a smart Router Helper which is maybe able to process your request based on your db defined routes.
Take a look into: net/http/Router.php
especially connect(), parse() and match()
I would start to write some kind of anonymous function and progress it to a testable Class which is located in /extension.. ?
Novice Zend Framework developer here trying to figure out a simple solution to a Zend Routing problem. I'm sure one of you pros can lend a hand.
I have a website (built in Zend Framework using Zend_Navigation) that contains 75% static HTML page content and a few controllers. Top level navigation is built in Zend_Navigation, looping through partials.
Because of my work I build a lot of sites along these lines (containing lots of static pages) so I want to get this right. I don't want to set up controllers and actions for each and every one of these static pages (there are many) and I wanted to create a solution where I used Zend_Controller_Router_Route to route all static content automatically through to a StaticController whose job it would be to include or render .phtml pages based on a controller/action pairing in the URL from some sort of directory like /layouts/staticpages
Because of SEO and various reasons I don't want to have the controller pairing in the URL for these static pages be visible as /static/page/page1... It has to be "real world descriptions" of the /section/page (eg. advantages/someadvantage )
Here is the problem: Using Zend_Controller_Router_Route can do the job when I set up the correct routes BUT it messes something awful with Zend Navigation... I assume because Zend_Navigaion doesn't play nice with on-the-fly controller/action switching.
Code example:
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route('advantages/:page/*',
array('controller' => 'static', 'action' => 'display', 'mode' => 'advantages',
'page' => 'index'));
$router->addRoute('advantages', $route);
This handles the job of switching pages in the "advantages" section well enough, but Zend_Navigation's automatic controller/action writing AND the highlighting of "active" nodes ends up being all screwed up because it now thinks that its controller is "static" and action is "display".
Is Zend_Navigation fundamentally incompatible with Zend_Controller_Router_Route? Is there a better way of doing this single static page controller or handling static content across the board?
Since you are using one controller/action for all static pages, you must customize your Zend Navigation before displaying it.
Check Example 4 in the Zend Documentation.
// the following route is added to the ZF router
Zend_Controller_Front::getInstance()->getRouter()->addRoute(
'article_view', // route name
new Zend_Controller_Router_Route(
'a/:id',
array(
'module' => 'news',
'controller' => 'article',
'action' => 'view',
'id' => null
)
)
);
// a page is created with a 'route' option
$page = new Zend_Navigation_Page_Mvc(array(
'label' => 'A news article',
'route' => 'article_view',
'module' => 'news', // required for isActive(), see note above
'controller' => 'article', // required for isActive(), see note above
'action' => 'view', // required for isActive(), see note above
'params' => array('id' => 42)
));
// returns: /a/42
$page->getHref();
I'm running my own extremely light MVC framework (without Router class).
To know what controller and action to run I'm using library or regexps applied to REQUEST_URI. Something like...
$regexps = array(
'/admin/categories/' => array(
'controller' => 'AdminCategories',
'action' => 'index'
),
'/admin/categories/add/' => array(
'controller' => 'AdminCategories',
'action' => 'add'
),
);
Everything works fine, BUT...
What is the best practice to BUILD urls?
BuildUrl('Controller', 'action', array('var1' => 'val1', 'var2' => 'val2'));
What is the best practice to modify current url (for, example I have table with 10 different filtering options)?
ModCurrentUrl(array(...));
How do you solve this problem in your applications?
PS: I'm not using conroller's action parameters, all parameters are taken from $_GET.
Thank you and sorry for my English.
Best practice ( as i see it ) would be to have a library called Routing ( basicly a class ) with static methods . You can then use Routing::buildUrl(...) , and fill it with all uri related methods you'll use ( like modCurrentUrl ... )
Here is couple good links for you:
http://robap.github.com/php-router/
http://code.google.com/p/routemap/
And i don't see any signs of subdirectories in your example. In big projects it could be fine.
Firstly request get to the router class' method, from there you get some actual data about the route. Secondly you put this data to a dispatcher which is starting your application.