Laravel 5 : Optional parameter in routes - laravel-5

I am looking for a solution for this code to work with the optional parameter.Indeed placed at the beginning, this parameter is no longer considered optional but I absolutely need to have a solution.
The goal is to archive the old versions of the website by year (it's for a music festival) without having to duplicate all laravel files ...
Route::get('{year?}/lineup', 'HomeController#agenda')->name('agenda');
In this precise case :
/2018/lineup => works
/lineup => not working
Note : I'm using Laravel 5.4
An idea, a suggestion to archive by date the old versions of the site ?
Archives must be accessible online.
Thank you

If you add them in this order it should first check for a match with the year and then for only the lineup. Then it is up to your controller (same for both routes, no extra files needed) to handle the input parameter, if it is set or not.
Route::get('{year}/lineup', 'HomeController#agenda')->name('agenda.history');
Route::get('lineup', 'HomeController#agenda')->name('agenda.current');

Related

Bitrix CMS, how to get cached data based on GET parameter in template of standart component?

I'm working with a component bitrix:catalog (which is standard one) and faced an issue. I want to add some extra GET parameters to switch view mode. I think there is no need to rewrite whole component to make such switcher, so I added extra keys in result_modifier in a way similar to:
$this->__component->arResultCacheKeys = array_merge($this->__component->arResultCacheKeys, array('key1', "key2"));
Earlier in the same result_modifier I perform adding those extra keys in $arResult['key1'] etc. They seem to be correctly saved, but only for current inquiry such as ?view=list or view=card, that means only one variable value is saved and it does not react on changing of GET parameter. Is there simple and correct way to make that component to cache and to output data based on GET variable? The only idea which came to my mind is to rewrite component by adding extra parameter and checking of GET, but I think there must more simple and correct solution to make in via template. Human Readable Links are turned on. And I want to have auto-cash being turned on as well. If I turn it off it starts working as planned.
One of possible solutions is to rewrite it cache by SetTemplateCachedData but it still seems to me rough and incorrect way for such simple task.
Bitrix masters please help me to find correct solution, google can't help at the moment.
If you use standard bitrix:catalog component, you may be use standart bitrix:catalog.section. In that component.php used standart component cache.
That means you can describe additional parametr in you custom .parameters.php, and set it in bitrix:catalog.section params.
Standart component cache set cacheId based on arParams.
So you include component should look like this:
$APPLICATION->IncludeComponent(
"bitrix:catalog.section",
"",
array(
"IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
"IBLOCK_ID" => $arParams["IBLOCK_ID"],
"ELEMENT_SORT_FIELD" => $arParams["ELEMENT_SORT_FIELD"],
"ELEMENT_SORT_ORDER" => $arParams["ELEMENT_SORT_ORDER"],
....
....
"NEW_ADDITIONAL_GET_PARAMS"=> $_GET['view']
),
$component
);
Of course better way somethink like
"NEW_ADDITIONAL_GET_PARAMS"=> (in_array($_GET['view'],array('list','card'))?$_GET['view']:'list')
But may be you need just set right catalog params: SEF_MODE SEF_FOLDER SEF_URL_TEMPLATES

Laravel 4 - some but not all routes failing on production

I have an application which has been running successfully for over a year on both the production server and my development machine (both running IIS without any pretty URLs). Yesterday I moved the latest in a series of (until now successful) updates from development to production, and since then many of the routes have been failing on the production server, even though they work fine on dev. I've verified that the exact same application and route files are present on both machines, I've confirmed that php artisan routes produce the same output on both machines, I've confirmed that their autoload_classmap.php files are identical, and I've verified that both servers are running Laravel 4.1.23 .
After some testing it appears that, for example, the URL https://localhost/EPHY/index.php/child/3958/edit, which triggers the ChildController::edit() RESTful method on dev, triggers ChildController::create() on production, which then crashes because it can't find the familyId parameter it's expecting. Here's the applicable line in routes.php on both servers:
Route::resource( 'child', 'ChildController', array(
'create' => 'child.create',
'store' => 'child.store',
'show' => 'child.show',
'edit' => 'child.edit',
'update' => 'child.update',
'destroy' => 'child.destroy'
));
I should note that Family, another RESTful resource, works successfully on both servers, but others don't. After doing some more testing, it actually seems like any routes relating to models that have an Eloquent relationship with Family are failing, and all the others are working fine. That's just weird...
Can anybody suggest what may be causing this, or at least where I can look? I'm happy to post additional information.
UPDATE: I continue to poke at this problem, and it's becoming apparent that this is definitely only affecting routes related to models with an Eloquent relationship to the Family model; all other routes in the application appear to be working correctly.
Ok, I figured this out, and it's lame. As I noted above, this was only affecting models which had an Eloquent relationship with Family. What that means in practice is that all of the ::create() methods of the associated Controllers needed the Family::id value. So, here's the code I was checking the determine whether I needed that value from Input::get() or Input::old():
$familyId = ( empty( Input::old() ) )? Input::get('familyId') : Input::old('family_id');
Anybody see the problem? empty() can't take a function call as a parameter. Everything worked fine as soon as I changed the code to:
$familyId = ( Input::old() )? Input::old('family_id') : Input::get('familyId') ;
Now, how did I figure this out? Well, after spending a day and a half assuming I knew what "Can't use function return value in write context" meant, I FINALLY CHECKED THE DAMN ERROR MESSAGE ON GOOGLE! Let this be a lesson to the newbs and a reminder to the veterans, never assume you understand what an error means without double-checking with the groupmind...
I'm so ashamed...

How to build CodeIgniter URL with hierarchy?

I know this doesn't exactly match the form of www.example.com/class/function/ID/, but what I want to display to the user would make more sense.
This is what I would like to do:
www.example.com/project/id/item/item_id/
So, an example would be:
www.example.com/project/5/item/198237/
And this would be the behavior:
www.example.com/project/ --> This would show a list of projects (current implementation)
www.example.com/project/5/ --> This would show a list of items on project 5 (current implementation)
www.example.com/project/5/item/ --> This wouldn't really mean anything different than the line above. (Is that bad?)
www.example.com/project/5/item/198237/ --> This would show details for item 198237.
So, each item is directly associated with one and only one project.
The only way I can think how to do this is to bloat the "project" controller and parse the various parameters and control ALL views from that "project" controller. I'd prefer not to do this, because the model and view for an "item" are truly separate from the model and view of a "project."
The only other solution (that I am currently implementing and don't prefer) is to have the following:
www.example.com/project/5/
www.example.com/item/198237/
Is there any way to build a hierarchical URL as I showed at the beginning without bloating the "project" controller?
There are 3 options, sorted by how practical they can be:
Use URI Routing. Define a regular expression that will use a specific controller/method combination for each URL.
Something like that could help you, in routes.php:
$route['project/'] = 'project/viewall';
$route['project/(.+)'] = 'project/view/$1';
$route['project/(.+)/item/'] = 'project/view/$1';
$route['project/(.+)/item/(.+)'] = 'item/view/$2';
That is, considering your controllers are item and project respectively. Also note that $n in the value corresponds to the part matched in the n-th parenthesis.
Use the same controller with (or without) redirection. I guess you already considered this.
Use redirection at a lower level, such as ModRewrite on Apache servers. You could come up with a rule similar to the one in routes.php. If you are already using such a method, it wouldn't be a bad idea to use that, but only if you are already using such a thing, and preferably, in the case of Apache, in the server configuration rather than an .htaccess file.
You can control all of these options using routes.php (found in the config folder). You can alternatively catch any of your URI segments using the URI class, as in $this->uri->segment(2). That is if you have the URL helper loaded. That you can load by default in the autoload.php file (also in the config folder).

CodeIgniter 2 not allowing multiple level subfolders for controllers

As I read the doc, controllers in CodeIgniter are supposed to support multiple level subfolders, but as far as I have tested, it is impossible to work after first a first level folder.
By example:
mysite.dev/ (index page, default controller home.php, works)
mysite.dev/admin/ (admin section, in admin/home.php, works)
mysite.dev/admin/manage/ (in admin/manage/home.php, do not work)
I am trying to know why and how to make it work on multiple level sub folders?
Thanks in advance!
CI only allows one sub-dir level. However, you can emulate this pattern with routes file as #Brendan says:
Controllers:
welcome.php
admin/admin.php
admin/manage.php
Routes file:
$route['admin/manage/:any'] = "admin/manage/$1";
$route['admin/admin'] = 'admin/home.php';
You can implement some changes to the hardcode to get works as expected: http://codeigniter.com/forums/viewthread/190563/

how to pass parameters to joomla module loaded from article

I had previously added Joomla modules from within Joomla articles this way : {loadmodule mod_name} but this time I need to pass parameters from it.
How can I pass parameters from within the article to a Joomla module?
You'll need to modify or clone the Joomla plugin loadmodule because it doesn't handle parameters other than just a module name. It's not particularly difficult to do if you're proficient in PHP (assuming you don't mind getting your hands dirty with a little bit of Regex work) but I'd suggest cloning it and using it this way:
Copy folder \plugins\content\loadmodule to \plugins\content\Myloadmodule (and all it's files)
Within the new folder, rename loadmodule.php and loadmodule.xml to myloadmodule.php and myloadmodule.xml. These are the files you'll do all the work on.
In both files, replace occurrences of loadmodule with myloadmodule, (Case sensitive)
In myloadmodule.php, start at around line 36 with the Regex that strips out what is in the {loadposition xxx} being processed. You'll need to tinker with this Regex to get the parameters that you want to supply when using {myloadmoduel blah-blah-blah} in your article.
Find the database entry in your table '_extensions' for loadposition and create and identical record for myloadposition. (Unless you want to write and installer)
Finally, you'll need to render the modules with your new parameters - I can't begin to help there because I don't know what modules, or parameter work you'll be doing, but this renderModule documentation will be of assistance.
7.
I think I've covered it all, but this should be enough to get most of it done for you. When you're done, use {myloadposition ...} instead of {loadposition ...}.
I will give more details about the previous answer, to be able to pass parameters to a module with a tag as {loadmodule mod_name,param}
The solution given by GDP works fine: it's easy and quick to rewrite a content plugin (e.g. myloadmodule), following the steps 1 to 5 in the previous answer.
The problem, for me, comes with the steps 6: how to put parameters into the module, and ohow to retrieve de parameters within the module.
Step 7 : how to give parameters to the "render" of a module
In the plugin created (base on the loadmodule), you have to find the lines with the following code :
echo $renderer->render($module, $params);
Before this line, you can put parameters into the $params parameter, but "render" of the module retrieves only params with the key 'params', using a json format.
So, to pass parameters to the render function, you can do something like that :
$param = array('param_name' => 'value'); // param_name = value
$params = array('params' => json_encode($param)); // 'params' (String) should be decoded by the render of the module
echo $renderer->render($module, $params);
Step 8 : How to retrieve the parameter within the module
In the helper of your module, you can retrieve the parameter with $params variable :
$value = $params->get('param_name');
To understand a helper, read this tutorial : http://docs.joomla.org/J3.3:Creating_a_simple_module/Developing_a_Basic_Module
I googled the same issue and found your question. I know its old but my find may help someone else. There are now plugins that allow embedding modules and allowing you to pass parameters to it. My choice is Module Plant.

Resources