Wildcard Prefix Laravel 5 - laravel-5

is there a way in Laravel 5 to have a recognized list of prefixes, such as ['gbr','en','eu'], so that
/gbr/bar/baz/shoelace // or
/eu/bar/baz/shoelace
is handled by the same controller#method as
/bar/baz/shoelace
Except that the additional parameter foo=gbr is passed in the first condition?
Note the Route::group prefix won't work because there may or may not be a prefix in this case. Also, this strategy should take precedence over all else, i.e. the Route would check for the (optional) prefix first.

Yes there is a way.
When declaring your routes, you can declare them as
Route::get('{prefix}/bar/baz/shoelace', 'controller#method')->where('prefix', 'gbr|en|eu');
gbr|en|eu is a simple regular expression that will match either the string gbr, en or eu. Check out Regular expression constraints for more details
And in your controller you can have
public function method($prefix) {
//code here
}

Related

In laravel, why provide the trailing . character in the prefix

In laravel, I had a question about the route name prefix.
The name method may be used to prefix each route name in the group with a given string. For example, you may want to prefix all of the grouped route's names with admin. The given string is prefixed to the route name exactly as it is specified, so we will be sure to provide the trailing . character in the prefix:
Route::name('admin.')->group(function () {
Route::get('/users', function () {
// Route assigned name "admin.users"...
})->name('users');
});
I don’t understand the meaning of this sentence:
The given string is prefixed to the route name exactly as it is specified, so we will be sure to provide the trailing . character in the prefix
In addition to increasing readability, I don’t know the essential difference between adding . and not adding .

xquery optional where statements

In Xquery 3.1 I am processing the variable parameters from a form to search for matching XML documents. The XML documents look like this:
<listBibl xml:id="TC0001" type="collection">
<bibl>
<title type="collection">Bonum universale de apibus</title>
<affiliation corresp="dominican"/>
<author nymRef="thomas_cantipratensis"/>
<location corresp="flanders"/>
<othercontent>....</othercontent>
</bibl>
</listBibl>
The user can submit optional parameters against xml:id, affiliation, author, and location, and they can be parameters with multiple values (sequences).
If the user were to submit all parameters, the query might look like:
for $c in $mycollection//listBibl[#xml:id=($params_id)]
where $c/affiliation[#corresp=($params_affil)]
and $c/author[#nymRef=($params_author)]
and $c/location[#corresp=($params_location)]
return $c
But the user may leave certain parameters empty, effectively making each where statement optional.
The only solution I can currently put together is to have a series of if...then...else statements which account for each permutation of parameters.
Is there any way in Xpath or Xquery to account for the parameters being empty with a wildcard of some sort? In pseudo code, where * represents a wished-for wildcard:
where $c/affiliation[if ($params_affil)
then #corresp=($params_affil)
else #corresp=* ]
Many thanks.
Use predicates of the form
[$params_affil=("", #corresp)]
which matches if $params_affil is either a zero-length string or equal to #corresp. And make zero-length-string (rather than empty sequence) the default if the parameter is not supplied.
Alternatively if the default for an absent parameter is (), use
[empty($params_affil) or $params_affil=#corresp)]
If that gets too repetitive, put the logic in a user-declared function.
I think you can always declare and use your own function as a predicate expression e.g.
declare function local:check-item($item as node(), $values as item()*) as xs:boolean
{
if (exists($values))
then $item = $values
else true()
};
....
where $c/affiliation[local:check-item(#corresp, $params_affil)]

Routing with 2 optional parameters where either one can be chosen

I am using a route with 2 optional parameters and I would like that either one of them can be chosen because they are used in a where clause. The where clause can be used on the first parameter OR the second one.
I tried this:
Route::get('activityperemployee/employee_id/{employee_id?}/month/{month?}', ['uses'=>'Ajax\ActivityAjaxController#getActivityPerEmployee','as'=>'ajaxactivityperemployee']);
but the problem is I can't find the page anymore if I don't set up both of the parameters.
The problem is the first parameter {employee_id?}. You cannot use it in this way becasue if You won't pass any params You'll get url like:
activityperemployee/employee_id//month
which won't find your route.
I think You should make required at least {employee_id} (without question mark) and always passing first parameter.
I suggest to use a get variable.
If you have multiple optional parameters
Route::get('test',array('as'=>'test','uses'=>'HomeController#index'));
And inside your Controller
class HomeController extends BaseController {
public function index()
{
// for example /test/?employee_id=1&month=2
if(Input::has('id'))
echo Input::get('id'); // print 1
if(Input::has('page'))
echo Input::get('page'); // print 2
//...
}
}
Hope this works for you! More information at https://stackoverflow.com/a/23628839/2859139

Spring request mapping wildcard exceptions

Can I put /** wildcard in a middle of request mapping such as: "/some/resource/**/somthing"
In Spring 3 I can do this
#RequestMapping("/some/resource/**")
to map
/some/resource/A -> ControllerMethod1
/some/resource/A/B -> ControllerMethod1
/some/resource/A/B/C/D/E/F -> ControllerMethod1
for any number of paths parts
However this mapping is too greedy and will not allow me to map a sub URL #RequestMapping("/some/resource/**/somthing") to another controller such as
/some/resource/A/somthing -> ControllerMethod2
/some/resource/A/B/somthing -> ControllerMethod2
/some/resource/A/B/C/D/E/F/somthing -> ControllerMethod2
How can i do this?
I thinks it's not possible to use that ant style in url mapping as you require, because it will stop on the next path separator character '/'.
I would suggest you to try 16.3.2.2. URI Template Patterns with Regular Expressions in order to map just the last part of the request (haven't tried this approach yet).
Also you can match the rest of the request using PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, and apply some expression there. Check this post.
Otherwise you should use request parameters to match that condition 16.3.2.6. Request Parameters and Header Values.
You can narrow request matching through request parameter conditions such as "myParam", "!myParam", or "myParam=myValue". The first two test for request parameter presense/absence and the third for a specific parameter value. Here is an example with a request parameter value condition.
In this case you will map something like that using params
#RequestMapping(value = {"/some/resource/**"}, params="somthing")
or use the annotation request parameter with not required attribute in method signature:
public void test(#RequestParam(value = "somthing", required=false) String str) {

Custom Query Parameters without Appending Them to URL

I have 'urlFormat'=>'path' and 'showScriptName'=>false in the urlManager.
I have proxies/read as controller/action and article=>some_name as parameters.
Whenever I create a link such as:
$this->createUrl('proxies/read', array('article'=>$name));
The result is an URL of the type:
proxies/read?article=socks5_proxy_list
I would like to dump the query parameter and reformat the URL to look like this:
controller/action/param_name/param_value
In this case that would be:
proxies/read/article/socks5_proxy_list
My current 'rules' look like this:
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/article/<article:\w+>'=>'<controller>/<action>/article/<article>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
But they don't seem to be working.
Shortcut to make this work:
$this->createUrl('proxies/read/article/'.$name);
And leave the urlManager rules without your custom rule.
Another way:
// urlManager
'rules'=>array(// order of rules is also important
'<controller:\w+>/<action:\w+>/article/<article:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
In the above array, i just put the new rule in the beginning, to make sure that, that rule is applied whenever such a pattern is encountered. If you have other rules, then just make sure that this rule appears before a more general rule, that can match the pattern. Rule of thumb: more specific rule should appear before general rule.
create url in view:
$this->createUrl('proxies/read', array('article'=>$name));
Incase you don't need any of the default "user-friendly url" rules, but only need path format urls, then you only need to specify 'urlFormat'=>'path' and leave the 'rules' array empty or omitted all together.
Read the URL Management guide in the definitive guide, if you haven't already.
no need for rules..
http://yourhost.com/mycontroller/dosomething/param1/value/param2/value
class MyController extends Controller {
public function actionDosomething($param1, $param2) {
}
}
as for createUrl(). hand it a key=>value array of parameters as a second parameter
http://www.yiiframework.com/doc/api/1.1/CController#createUrl-detail
try these set of rules
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
),

Resources