zend routing works only in lowercase letters - php

I'm setting up routes in application.ini, so when I try to access /moved, it displays cont/move. It works but only if I type moved all lower letters exactly like it's setup on the first line. How can I make Moved or moVed or any other letter combination also work? Do I need to do it in Bootstrap to get finer control and how?
routes.test.route = moved
routes.test.defaults.controller = cont
routes.test.defaults.action = move

This is not a wise approach.
URLs are case sensitive for a reason. You will get duplicate content penalty from search engines. Users will be confused too.
However, you may create controller plugin to achieve this:
public function preDispatch()
{
$this->getRequest()->setControllerName(
strtolower($this->getRequest()->getControllerName());
)->setDispatched(false);
}

I've searched Google for a few minutes, and this page (http://joshribakoff.com/?p=29) covers a nice patch. This patch overrides the request object, instead of the dispatcher or the router.

Related

Disable Cache in TYPO3 in a USER_INT function

I am working on a TYPO3 project where I have to dynamically disable caching based on a condition. It is a very specific usecase, that will not happen a lot.
I planned to use a USER_INT function, where I would perform the check and disable the cache if necessary. The USER_INT function works flawlessly, it is being called on every page load.
The thing is, I can not disable the cache, or at least I do not know how.
The code, I have right now:
page = PAGE
page {
typeNum = 0
adminPanelStyles = 0
11 = USER_INT
11.userFunc = [COMPANY_NAMESPACE]\PageHandler->checkCache
And in the function I perform the check:
public function checkCache($content,$conf){
global $TSFE;
$id = $TSFE->id;
if($this->checkIfDisableCache($id)){
//$TSFE->set_no_cache(); // <---- first I tried this one
$TSFE->no_cache=true; // <-----after a while I got despoerate and tried to disable it directly
}
}
I also tried to play with the config, it did not work.
The funny thing is, if I set it directly in typoscript:
config.no_cache = 1
it works, but since the check is rather complex, I want to use PHP to determine, if the cache should be disabled.
I know I am doing something wrong, I just don't know what. Any help would be appretiated :)
I don't think either of the previous answers really explain the situation. You have sort of a catch-22 here, in that your USER_INT is executed after the page cache entry has been generated. The way it works internally is everything that can be cached gets rendered first, and every USER_INT then outputs a marker in the HTML source which gets replaced afterwards. This way the cache can contain the version with markers and those can be rendered without having to render the whole page.
So what you need to do in this case if you want the page cache to be disabled only in some conditions, is to use a custom TypoScript condition that is capable of setting config.no_cache = 1 only under special circumstances. That way you prevent generating a cache entry if the condition is met, but preserve full caching and cached output for every other request.
https://docs.typo3.org/typo3cms/TyposcriptSyntaxReference/TypoScriptParserApi/CustomConditions/Index.html
Note that it is still recommended that you instead create the parts of your page that must not be cached, as USER_INT objects. Having a use case where you in some cases need to disable the entire page cache indicates a possible misunderstanding of how the caching framework and/or USER_INT works. Hopefully the above explains those parts a bit.
if you look at the pibase (AbstractPlugin) code you will see that probably setting $conf['useCacheHash']and $conf['no_cache'] should be done.
https://api.typo3.org/typo3cms/current/html/_abstract_plugin_8php_source.html#l00190
If you create this object as USER_INT, it will be rendered non-cached, outside the main page-rendering.
https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/UserAndUserInt/Index.html

How to call TYPO3 plugin when normal page renders

Well, I am developing a plugin a and I need to display some stuff from my plugin when TYPO3 page load.
Is there some function how to hook action, like in WordPress when page loads than plugin will execute some controller method? Then this controller will produce some output a HTML, which I would like to dispaly in frontend page. Specially I would like display custom script in the head. So the script should be like this <head>...<script>my content</script>...</head>
Ok, what you probably want to do is to develop a so-called TYPO3 extension - that's what plugins/add-ons are called in TYPO3 (which is the term you will likely find google results for).
To get started fast you can try the TYPO3 extension builder (https://docs.typo3.org/typo3cms/extensions/extension_builder/) - which can generate a skeleton extension for you.
For more information you can also have a look at https://docs.typo3.org/typo3cms/CoreApiReference/latest/ExtensionArchitecture/Index.html which explains the concepts in far more detail.
Additional information is available in https://docs.typo3.org/typo3cms/ExtbaseFluidBook/Index.html
in TYPO3 there is something named plugins, but you should differ to the meaning in other context.
first TYPO3 is a CMS which content is structured in a hierarchical tree of pages. These pages are the basis for navigation. and each page contains individual contentelmenents (CE).
As Susi already told: add ons to TYPO3 are in general 'extensions' which could extend(!) the functinality of TYPO3 in different ways. one way is the definition of (TYPO3-)Plugins. These are special ContentElements which enable to show special information.
While normal CEs have all the information what to show in the record (e.g. Text & Image), plugins can be more flexible.
typical examples are: show a list of records OR one record in detail.
These Plugins can be controlled with typoscript or the plugin-CE could have additional fields to hold information what to display.
For detailed information how a plugin is defined consult the links given by Susi.
And be aware: for security reasons it is not possible to just execute a plain PHP file to echo any output. You need to register your plugin using the API, build your output as string and return the generated HTML as string to the calling function. For beginners the ExtensionBuilder will help you to generate a well formed extension which uses the API to register and output your data.
OK guys, thanks for your answers, but it was not very concrete. I found this solution, is not the best one, but it works! If anybody has better please share.
At first, you have to make a file for the class which will be called from the hook at location /your-plugin-name/Classes/class.tx_contenthook.php. Filename have to have this pattern class.tx_yourname.php Inside we will have a code with one method which will be called by the hook.
class tx_contenthook {
function displayContent(&$params, &$that){
//content of page from param
$content = $params['pObj']->content;
//your content
$inject = '4747474747';
// inject content on
$content = str_replace('</body>', $inject. '</body>', $content);
// save
$params['pObj']->content = $content;
}
}
And next, we have to call it on the hook. So Let's go to /your-plugin-name/ext_localconf.php and add there these two lines, which makes a magic and handles also caching.
// hook is called after caching
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-output'][] = 'EXT:' . $_EXTKEY . '/Classes/class.tx_contenthook.php:&tx_contenthook->displayContent';
// hook is called before caching
$TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['contentPostProc-all'][] = 'EXT:'. $_EXTKEY .'/Classes/class.tx_contenthook.php:&tx_contenthook->displayContent';
I hope this will help those who struggling with typo3.

"Dynamic" rewrite with mod_rewrite and apache

I´ve a bit of a "strategic" problem with creating a nice mod_rewrite solution for a shop. The shop has different kind of pages:
Products (products.php)
Categories (category.php)
Content (content.php)
So the "default" URI´s look like this:
/products.php?id=2
/category.php?id=4
/content.php?id=5
The final result should be a URI´s that include the name of the page (product name, category name).
/chocholate-icecream -> product.php?id=2
/coffe-and-ice -> category.php?id=4
/imprint -> content.php?id=5
My problem now is, that I´ve different types of content which maps to different .php files. And I´m unable to determine to which .php file to route with mod_rewrite as long as I don´t use a cheat "like **/product/**chocolate-icrecream" so that I know that this URI is made for a product. But me and my customer don´t want that solution.
I´ve already started another try:
ALL requests which don´t match a physical file or folder are routed to a mapper.php file. This file looks in the database - I´ve an index with all products, categories etc. - and gives me the correct file.
The mapper then loads the content via CURL and shows it to the user
Basically I thought this was a nice idea but there are so many problems with session handling, , post data, php based redirects (header: ...) with CURL that I really plan to cancel this way etc. etc.
So my question is:
Has anyone of you an idea or a pattern that helps me creating the "good looking" URI´s without using a complex CURL mapper?
Thanks a lot in advance,
Mike
UPDATE to "Include solution":
A good example for the "include problem":
A request for www.shop.com/cart is "rewritten" to "mapper.php". The "mapper.php" decides to include www.shop.com/shoppingcart.php.
shoppingcart.php uses smarty to display the cart. The view asks the $_SERVER variable, if the actual called file is "shoppingcart.php" to decide if the page should be shown in fullscreen mode or with additional columns.
In the "normal / direct" call $_SERVER['PHP_SELF'] would be "/shopping_cart.php" which will show the fullscreen version. Going over the mapper.php with the include option would give us $_SERVER['PHP_SELF']=='/mapper.php' which will give us the view with columns (what´s wrong).
You could do this simply by pointing them all to one page and then including the proper script based on the slug. For example, rewrite all requests like index.php?slug=chocolate-icrecream.
index.php
$resource = fetch_resource($slug);
if ($resource->slug == 'product')
{
include('products.php');
}
else if ($resource->type == 'category')
{
include('category.php');
}
else
{
include('content.php');
}
I don't understand why you don't want to structure your URLs like /product/chocolate-icrecream. That would be good semantic naming that is fairly consistent across the internet nowadays.

Change controller id

I want a piece of code on the index of my Code Igniter's script so I can change the identety or name of my controller from agent69 to agent007_and_supergirl,and also a way to beat any rule that pages might have against using underscore to separate words as an amendment.
In other words I want that all the calls that the processor of my server has inside my code igniter for agent69 be translated to calls to agent007_and_supergirl
Is just a change of identety for the controller without altering the main controler's functionality and without getting my hands dirty with coding.
If you want re route your default controller url, you have to edit routing configuration like this:
in application/config/route.php
$route['agent007_and_supergirl'] = 'agent69';
Now agent69 Controller will be accessible by http://youriste.com/agent69 and http://yoursite.com/agent007_and_supergirl as well
CodeIgniter URI Routing Guide
Working on config/routes.php yelded miserabe results on past,remember that is an identity matter
And I want all the calls of agent69 be sent to agent007_and_supergirl that is its new identity
What was successfull on the past about this mattar was controller and file cloning,it worked masterfully
but this only intercepted few server calls so for me to get all the server calls, I need to clone every
single fie on the script so I feel silly teling you again thats why I didnt want to get my hands dirty with code
because is plenty of code.
Well hope that you can help me master this code that goes on the index of my code igniter script--///The code cant appear here so its below in the comment to lighta thanks 4 readin

Context-aware AJAX call in a modular site

Edit: I thought about a possible solution, but I made another question as it is very specific: see AJAX proxy with PHP, is it possible?
A couple of times I've encountered this problem...
I create sites that have a certain degree of modularity. So, it is possible that there are "components" (think of a rough CMS) which carry their own PHP code, CSS, and JavaScript, all dynamically included. Think about a structure like:
{siteroot}/component/datagrid/datagrid.php
{siteroot}/component/datagrid/js/datagrid.js
{siteroot}/component/datagrid/css/datagrid.css
{siteroot}/component/datagrid/ajax/getsomedata.php
Now, the question is: for JavaScript files, and expecially AJAX calls, how do I make them context-aware with the URLs?
For example, if in datagrid.js I want to call siteroot/component/datagrid/ajax/getsomedata.php with AJAX I should write (with JQuery):
$("#ajax").load("siteroot/component/datagrid/ajax/getsomedata.php");
First problem: siteroot changes on different installations. I've managed that by including a general
var codeBase = <? echo json_encode(Config::$siteRoot); ?>
with PHP on every page, from a Config file that can be easily edited for every installation, so I can do with whatever JavaScript something like:
$("#ajax").load(codeBase + "/component/Datagrid/ajax/getsomedata.php");
What do you think of this approach?
Second problem: but I have PHP functions that return to me also the components folder, or the folder of other components. It would be nice to make the whole URL dynamic. This would account also for changes in the structure of the component if I want.
The only solution I've found is to use a .js.php dynamic Javascript. This is very unelegant, and I have to include all the framework in the JavaScript file, like:
<?php
include "../../libs/framework.php"; // get my functions...
$myUrl = Config::$siteRoot . Framework::getComponentAjaxDir("datagrid") . "/getsomedata.php";
?>
$("#ajax").load(<?=json_encode($myUrl)?>);
Another side effect is that I have to know exactly the include the path for framework.php... I don't want this so hard-codedin my ".js.php" file.
Any smart solutions about that?
As nobody answered in a suitable way, I answer to myself to provide a solution I've found out that can be useful.
The key to my solution is simple:
I create an AJAX proxy at a fixed location in my site structure, so I can use codeBase to reference the proxy from JavaScript
I call this proxy with two parameters: plugin and action, which identify a) the plugin folder in which the "real" ajax is and b) the ajax file to use, along with the other params:
$("#...").load( codeBase + "/main/ajax.php?plugin=Datagrid&action=gettable&otherparams"...)
In ajax.php I sanitize the parameters, and use plugin and action to obtain the "real" ajax file:
{serverRoot}/components/{plugin}/ajax/{action}.php
Then i simply include that file in ajax.php
To be honest your problems are realistic options and aren't that bad practice in general quite frankly.
But let's explore this a little further.
What would be the best approach is for you to have 1 main config.php file which you can then specify modules, i.e. your datagrid etc.
You could store all modules in an array variable like so:
$_SITE_PATH = "/var/www/html/";
$_HTTP_PATH = "http://example.com/";
$_MODULES_PATH = $_SITE_PATH."modules/"
$_MODULES = array(
"datagrid"=>$_MODULES_PATH."datagrid/init.php",
"something_else"=>$_MODULES_PATH."something_else/init.php"
);
Each module would have it's own directory with instantiation init.php so that it would load all it required to get going.
This way you could code as you liked and when you needed something (preferably in the header state) do something like this.
global $_MODULES;
require_once($_MODULES["datagrid"]);
Everything will be easily available as and when required without any variable path issues later down the line.

Categories