Define a singular rule in the bootstrap for Inflector - php

I'm using CakePHP 2.1 and need to define an Inflector rule for the word "Software", because CakePHP is converting all references to the plural form "Softwares" which isn't correct. Cake is looking for SoftwaresController and a table named Softwares.
I do know to create the rule in the boot strap, and read this doc reference.
http://book.cakephp.org/2.0/en/development/configuration.html#inflection-configuration
I also took a look at the lib/Cake/Inflector.php file, but can't figure out the syntax for defining a rule. It looks kind of like regex. Here are a few rule examples.
'/(s)tatus$/i' => '\1\2tatuses',
'/(quiz)$/i' => '\1zes',
'/^(ox)$/i' => '\1\2en',
'/([m|l])ouse$/i' => '\1ice',
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
What would be the correct code to define a Software singular Inflector rule?
EDIT:
Inflector::rules('singular', array('rules'=>array('/software/'=>'software'),'irregular'=>array('software'=>'software'),'uninflected'=>array('software')));
I tried adding this rule, which works for the SoftwareController but Cake is complaining that it can't find the Softwares table, which is actually named "Software". I feel I'm close, but still missing something about how this works.

you just have to know where to look (or search) in the book:
http://book.cakephp.org/2.0/en/development/configuration.html#inflection-configuration
in your case
Inflector::rules('singular', array(
'uninflected' => array('software')
));
Inflector::rules('plural', array(
'uninflected' => array('software')
));

Related

Yii URL Manger create a route with multi optional parameters

I have a URL rule with multiple optional parameters and it was working but it stopped after I upgraded from Yii 1.1.15 to 1.1.19.
const OPTIONAL_PARAMS = '(/<featured:featured>)?'
. '(/subType/<subType:.*?>)?'
. '(/type/<type:\d+>)?'
. '(/category/<category:.*>)?';
'<lang:(en|fr)>/reports'. OPTIONAL_PARAMS => 'reports',
Any one can provide me some ideas or someone faced similar issues?
P.S. The other URL manager rules are working fine, only this one with (SOME_CODE)? for optional params is not working. I'm using PHP 5.6 and Apache.
You may be interested in by this issue. But in short: this syntax (regexp outside of named params) was never officially supported and it was removed as a bugfix in Yii 1.1.17.
The last version which supports this is 1.1.16, but it is really old and I would not recommend using it. You should probably create custom UrlRule and use it instead of CUrlRule for this particular case.
You may also try to add /* at the end of the pattern, like this:
'<lang:(en|fr)>/reports/*' => 'reports',
This will allow to append GET params to URL as /key/value. So this:
$this->createUrl('reports', [
'type' => 'sometype',
'category' => 'somecategory',
'lang' => 'en',
]);
will create URL like:
/en/reports/type/sometype/category/somecategory

Custom URL rules with modules in Yii2

I have been looking around but haven't found what I needed. Basically, I have a few small modules which have just the DefaultController and a few bigger ones with multiple controllers.
My rules for the small modules work fine but the ones of the big modules won't. Here are my rules:
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
'<module:\w+>/<action:\w+>/<id:\d+>' => '<module>/default/<action>',
'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>'
The first two rules work fine, allowing me to access:
http://host/news/create and routes to news/default/create.
The last two are supposed to do the following:
http://host/posts/category which should route to posts/category/index
and
http://host/posts/category/create which should route to posts/category/create
They do not seem to work, sadly. Any suggestions?
It looks like the first rule will capture any request that could also match the third one.
Think of it in the terms of its representing regex: w+/w+: as a generic rule for routes in Yii, more prescriptive rules should go on top and less more generic, catch-all rules should be at the bottom.
Now the best way to obtain what you need would be to do something along the lines of:
'<module:news>/<action:\w+>' => '<module>/default/<action>',
'<module:news>/<action:\w+>/<id:\d+>' => '<module>/default/<action>',
'<module:posts>/<controller:\w+>' => '<module>/<controller>/index',
'<module:posts>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>'
this way you are explicitly expressing the routes for each of the modules in a clear and immediate way which will also help you in the long-term.

Cakephp Routing

I want that users can surf to http://www.yyy.com/xxx for xxx being a parameter. and so with www.yyy.com/xxx/zzz . I have following routing which works fine:
Router::connect('/:town', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town')));
Router::connect('/:town/:category', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town', 'category')));
But when I want tot surf to a different controller example www.yyy.com/differentcontroller/add it goes back to the places controller unless I make a routing for it...
any ideas?
The second rule on your routing list is simply looking for two sets of any combinations of characters after the domain name and treating the first as a town and the second as a category. As a result it mistakenly parses 'differentcontroller' as a town name and 'add' as a category. If you want to keep this URL structure then you'll need to add more specific routes to your routing file to cover situations like the 'add' route, or consider changing your existing URL layout to something more specific, like:
Router::connect('/places/:town', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town')));
Router::connect('/places/:town/:category', array('controller'=>'places', 'action'=>'index'), array('pass' => array('town', 'category')));
My memories about cake routing is quite rusty, but you can use :controller/:action as a rule to make cake look for a valid controller/action pair, if I remember correctly.

CakePHP 1.3 inflections?

Where to put custom inflections in CakePHP 1.3?
inflections.php has been removed in CakePHP 1.3, I have taken a section out of the 1.3 migration guide which should explain:
Loading custom inflections
inflections.php has been removed, it was an unnecessary file hit, and the related features have been refactored into a method to increase their flexibility. You now use
Inflector::rules() to load custom inflections.
Inflector::rules('singular', array(
'rules' => array('/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta'),
'uninflected' => array('singulars'),
'irregular' => array('spins' => 'spinor')
));
Will merge the supplied rules into the infection sets, with the added rules taking precedence over the core rules.
Source
Its under /app/config/inflections.php.

PHP Application URL Routing

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...

Categories