Kohana 3 experts, handling request arguments Route::set(), request->params() in K3 v. >= 3.1 - kohana-3

Originally in Kohana 3 you were able to pass controller action arguments/parameters through URL as in:
http:/website/controller/actionname/param1/param2/.../paramX
and handle it by simply defining the action as in:
public action_actionname($params)
{
$params_array = explode("/", $params);
//you can now use $params_array[0], $params_array[1], ...
}
Now it seems that since v3.1 they decided to deprecate this feature (here is the link) and it should be eliminated in v3.2
And it seems they want you to use Route::Set() and request->param() methods instead. Does this mean that every time you define a method/action in a controller, you have to define a separate routing for each argument someplace else in your application?
Can anyone please explain to me how this works in simple terms. It just seems like a lot of unnecessary "hassle" to do all of that to simply call a function.

Maybe you should consider using the regex param in your route to override the default matching behavior... I typically use this to capture multiple URL parameters in one KO3 "param". Example:
Route::set('route1', '<controller>/<action>(/<param_list>)', array('param_list'=>'.*'))
->defaults(array(
'controller' => 'my_default_controller',
'action' => 'my_default_index'
));
Now in your controller, $this->request->param("param_list") will equal whatever matches the regex expression .* which means you can capture the rest of the URL just like you were hoping, with slashes and all!

Related

How to use the build in CRUD-Modell for insert and escape the datas on Codeigniter4

I tried to understand the new ways and possibilities with Codeigniter4.
I see a shorter way by the build in Crud-Model to do the simplest job with a bit less of code.
Do I insert new datas in the controller with this code, after I setup the myModel with the protected variables.?
$this->myModel->insert(['filed1' => 'value1,
'field2' =>$this->request->getPost('field')...
]));
So that works fine.
Now I want to make it a bit more secure and I want to insert only escaped values.
For that CodeIgniter has a lot of built-in functions/helpers. So I try to use "escape()" in this way
$this->myModel->insert(escape(['filed1' => 'value1,
'field2' =>$this->request->getPost('field')...
])));
but it failed with the error "Call to undefined function App\Controllers\escape()"
So how I can insert only escaped values into my db with the nicest/fastest code/Way may which is supported with the build-in basic Crud functions?
Thanks to teach/help me in this point!
escape doesn't work in a global context because that method is a member of the Database class (or rather, a parent class that implements the ConnectionInterface interface).
esc is a global function, which is why that works in a global context.
However, esc is designed to escape data that's going into web pages, not databases.
The good news is, if you're using Query Builder methods, then input is already escaped for you automatically:
It also allows for safer queries, since the values are escaped
automatically by the system.
If for some reason you still need to manually escape input (e.g. using basic queries), there are a few options, including the escape method you were trying to use earlier.
I find a first way with "esc()"
$this->myModel->insert(['filed1' => esc('value1'),
'field2' => esc($this->request->getPost('field'))...
]));
maybe there is a better way or someone has another sugeestion?

Kentico MVC culture two letter code in URL pattern

I'm trying to create a more friendly page URL in Kentico with the two letter culture code (en) instead of the full culture code (en-US). I modified the page URL pattern as seen in the image below. Is there a better way of doing this, should I create a custom macro expression?
The other thing I wanted to achieve was having the default culture not in the URL. Maybe I could also use the custom macro expression for it.
I have solved the problem with a custom macro expression. This is used in the URL pattern in combination with the DocumentCulture. The macro expression takes the culture alias name which is in case of the default culture empty and else the two letter code, and returns this with a '/'.
The url pattern for a page:
{%Util.GetUrlCultureCode(DocumentCulture)%}{%NodeAliasPath%}
The macro expression I wrote for fixing this problem is as below.
public class CustomCultureMacroExpression : MacroMethodContainer
{
[MacroMethod(typeof(List<string>), "Returns the url culture code of current document", 1)]
[MacroMethodParam(0, "Culture", typeof(string), "Current culture.")]
public static object GetUrlCultureCode(EvaluationContext context, params object[] parameters)
{
// Branches according to the number of the method's parameters
switch (parameters.Length)
{
case 1:
return GetCultureAliasOfCulture(parameters[0].ToString());
case 2:
// Weird bug causing macro expression in url pattern to have two parameters where the first parameter is null.
return GetCultureAliasOfCulture(parameters[1].ToString());
default:
// No other overloads are supported
return string.Empty;
}
}
private static string GetCultureAliasOfCulture(string cultureCode)
{
var culture = CultureInfoProvider.GetCultureInfo(cultureCode);
var culturealias = culture?.CultureAlias ?? string.Empty;
return string.IsNullOrEmpty(culturealias) ? string.Empty : $"/{culturealias}";
}
}
You may try to update culture codes in Localization -> Cultures, however I'm not sure if it won't have any side effect
If you use the Dynamic Routing module, you can hook into the GetCulture and GetPage global events to do what you are looking for.
For the GetCulture, you would check if the URL has a 2 letter culture code, if it does then use that as the culture.
You can also adjust the GetPage if needed, although the URL slugs generated should work properly, unless you want a fall back that on the GetPage.After, if there is no found page, you can try removing any culture from the URL and looking up the page by that.
I recommended using the NodeAliasPath for the remaining part of the UrlPattern for ease of use.

passing a parameter from a content type to a module

I assumed this would be easy but I am stumped.
I have a custom content type that includes an id field. I render these with a basic page template.
I have written a small module that creates a block which is populated with data from an external API. Everything works except I cannot seem to figure out how to pass the value of the id from the content of a given page to my module so it can make the API call.
It would be a couple of lines of code in straight php, it can't be that complicated in Drupal 8 and twig can it?
I managed to find a solution here
I am re-posting it in case it is useful to anyone else.
If you are generating a custom block you can access content fields via the routing system inside your block build function like this:
public function build() {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$field_my_custom_value = $node->field_my_custom_value->value;
}
//do something with the variable, like make the API call
//Make sure to set the cache to the context or even to zero if you need
return array(
'#markup' => $this->t('my content to render'),
'#cache' => array(
'contexts' => ['contexts' => ['route']],
),
);
}
I think you can reach what you want with a HOOK_preprocess.
use:
YOUR_MODULE_preprocess_node(&$variables){ ... } or
YOUR_MODULE_preprocess_block(&$variables){ ... }
to access your variable from the content type and pass it to your function oder template.

How to write a route that targets an action from a path paramater

If I have multiple urls like '/some/url/path/foo/bar1', '/some/url/path/foo/bar2', '/some/url/path/foo/bar3' etc. How can I write a single route that will direct the urls to the foo controller and the action of bar1, bar2, bar3 etc.
EG. (only this doesn't work.)
'/some/url/path/foo/:bar' : 'foo.:bar'
There are different ways to do this. I might suggest that you don't need the different actions in your controller, but just have different cases in a single action that respond to a url param (ex bar). That would be more inline with conventions.
However, you can accomplish what you want with the following.
Create your route
'/some/url/path/foo/:action': {
controller: 'FooController',
action: 'getAction'
}
Then in that controller create a getAction method. This method can now use req.param.action variable to find an execute the corresponding action.
FooController.getAction = function(req,res,next){
return sails.controllers.foo[req.param.action](req,res,next)
}

How not to repeat myself in Kohana 3's routes?

Let's say I want to make a system which can afford a multilingual web project. The system will consist of the modules that are put in Kohana's standard directory modules. Let's say that the standard access to the particular language can be done via lang parameter (i.e. somesite.com/en/somepage). The problem is that I have to repeat myself in defining my modules routes prepending each uri with (<lang>). Is there any way to avoid that? I thought about a separate language route declaration (for example in bootstap.php file), but I guess it won't solve the problem.
It's all about Kohana 3. Thanks to all.
UPDATE:
I think that the way suggested by The Pixel Developer is what one need if some part of the rule in route repeats everywhere.
Move up a level and extend the route class.
http://github.com/kohana/core/blob/master/classes/kohana/route.php#L69
public static function set($name, $uri, array $regex = NULL)
{
return parent::set($name, '(<lang>)'.$uri, $regex);
}
Not tested, but that's the general idea.
If lang is required in route, why don't you just put it in the default route? Surely that's the easiest way to go about. Something like:
Route::set('default', '<lang>(<controller>(/<action>(/<id>)))', array('lang'=> '[a-z]{2}'))
->defaults(array(
'controller' => 'somepage',
'action' => 'index',
'lang' => 'en',
));
Where lang is any 2 letters alphabets which defaults to 'en'.

Resources