Convert a named route to a hash (#) in CakePHP - html

In CakePHP, I want to convert this URL
example.com/FAQ/What-came-first-the-chicken-or-the-egg
to
example.com/FAQ#What-came-first-the-chicken-or-the-egg
using the routes.php and have the browser scroll to that anchor.
I tried this:
Router::connect('/FAQ/:faq',
array('controller' => 'pages', 'action' => 'faq', '#' => ':faq'),
array('faq' => '[A-Za-z-_]+')
);
If I then do debug($this->request->params), is says
array(
'plugin' => null,
'controller' => 'pages',
'action' => 'FAQ',
'named' => array(),
'pass' => array(),
'faq' => 'What-came-first-the-chicken-or-the-egg',
'#' => ':faq'
)
and the browser doesn't scroll anywhere.

Not possible
A webserver has no visibility whatsoever of the url fragment, as such routes based on the url fragment will never work.
Your route needs to match the received url
If you request the url /Foo/#hash in a browser, the server will only receive /Foo/ - this is the url that cake sees, this is the url that must match a route if you don't want to see errors. As such your route needs to be:
Router::connect('/FAQ',
array('controller' => 'pages', 'action' => 'faq')
);
Incidentally that's an odd route - the pages controller comes with a dynamic display function, it's not normal to create actions in this controller, rather use it like so:
Router::connect('/FAQ',
array('controller' => 'pages', 'action' => 'display', 'faq')
);

I have not tested it:
Router::connect('/FAQ/#:faq',
array('controller' => 'pages', 'action' => 'faq'),
array(
'pass' => array('faq'),
'faq' => '[A-Za-z-_]+'
)
);

Related

Multiple categories in Url

I want to create links something like that:
http://example.com/cat1/itemname-1
http://example.com/cat1/cat2/itemname-2
http://example.com/cat1/cat2/cat3/itemname-3
http://example.com/cat1/cat2/cat3/[..]/cat9/itemname-9
How rule looks like in yii2 UrlManager and how to create links for this?
Url::to([
'param1' => 'cat1',
'param2' => 'cat2',
'param3' => 'cat3',
'slug' => 'itemname',
'id' => 3
]);
Above code is really bad for multiple category params.
I add that important is only last param it means ID.
Controller looks like that:
public function actionProduct($id)
{
echo $id;
}
The below url rule would to this trick but you have to build the "slug" with the categories within your controller:
'rules' => [
['route' => 'module/controller/product', 'pattern' => '<slug:(.*)+>/<id:\d+>', 'encodeParams' => false],
]
Generate the Url:
yii\helpers\Url::toRoute(['/module/controller/product', 'slug' => 'cat1/cat2/cat3', 'id' => 1])
The output would be:
example.com/cat1/cat2/cat3/1

yii2 routing - pass parameter to route in rules

When a user accesses domain/page, I need to route them to controller/action/100.
I don't want to pass any parameter through the URL, but want to add it in url rules.
I added the code below to my config file.
'urlManager' => [
'rules' => [
'login' => 'site/login', // working
'about' => 'cms/page/10' // Not Working
'about' => 'cms/page?id=10' // Not Working
],
],
The first rule is working fine.
Can I pass the parameter for the route in url rules?
You need to use defaults and declare the rule explicitly:
'urlManager' => [
'rules' => [
'login' => 'site/login',
[
'pattern' => 'about',
'route' => 'cms/page',
'defaults' => ['id' => 10],
]
],
],
Add 'mode' => \yii\web\UrlRule::PARSING_ONLY to this rule if you want to prevent the transformation when you create a URL with the UrlManager (e.g. Url::to() uses the UrlManager and its rules and works in the opposite direction, that is Url::to(['cms/page', 'id' => 10]) will generate a link about)
Also consider to configure a redirect at your web server instead.

Customize prestashop web service

I want to customize Prestashop web service for my own usage but I don't know how and I can't find any tutorial. I have a mobile application that want to retrieve data from website but the default web service is useless.
For example I want the list of categories (in a language) with they're pictures but It seems I should call two different service to retrieve categories and images separately.
Assume I want to have a JSON array of categories that a category is a JSON object that have these fields {id,title,imageUrl} but It seems I should get {id,title} with a method and after that I can get images on by one by another method!
I couldn't find any guide for extending or customizing web service in the documentation.
I'm a bit late but:
Prestashop version requier : > 1.6
If you want to customize a web service and return specific fields with a JSON format output. You need to do it this way:
First
In override :
Create a class : myClassForWs that extends myClassCore
Override WebserviceRequest in webservice/WebserviceRequest.php
In webservice/WebserviceRequest.php : Add myClassForWs to
ressources:
public static function getResources()
{
$resources = parent::getResources();
$resources['myClassForWs'] = array('description' => 'The class','class' => 'myClassForWs');
ksort($resources);
return $resources;
}
}
In myClassForWs : redefine $webserviceParameters and $definition with the fields you need:
protected $definition = array(
'table' => 'category',
'primary' => 'id_category',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
'name' =>array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'link_rewrite' =>array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'description' =>array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
),
);
protected $webserviceParameters = array(
'objectsNodeName' =>'categories',
'hidden_fields'=>array('nleft', 'nright', 'groupBox'),
'fields' => array(
'level_depth' => array('setter' => false),
),
'associations' => array(
'images' => array(
'resource' => 'image',
'fields' => array('id' => array())
),
),
);
Then
Go in your admin tab :
in performance : clear cache
in advanced settings > Web service: active a api Key and set myClassForWs for this key
Finally
Access to your web service with url :
my.prestashop/api/myClassForWs/{id_class}?output_format=**JSON**
And it returns your datas
I hope it helps.

CakePHP basic auth on API (json) request

I want to make a request to resource/index.json, but since I index is not allowed without authentication it redirects me to login page. That's the behavior I want when no username:password has been sent
The thing is how do I set AuthComponent to work with both Form and Basic and only check for basic when the request goes through api prefix.
Also, does it automatically authenticate when found username and password in the header or do I have to do it manually?
in respective controller add few lines
class NameController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow("index");
}
}
This will allow index without authentication.
I decided to use Friend's of Cake TokenAuthenticate, and yes, it works along with FormAuthenticate so I am able to use both.
As a matter of fact, it automatically chooses the component it's going to use based on if there is an existing _token param or a X-MyApiTokenHeader header.
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form',
'Authenticate.Token' => array(
'parameter' => '_token',
'header' => 'X-MyApiTokenHeader',
'userModel' => 'User',
'scope' => array('User.active' => 1),
'fields' => array(
'username' => 'username',
'password' => 'password',
'token' => 'public_key',
),
'continue' => true
)
)
)
);

Magento Admin Create and Save HTML to Database With a Form Field

I am working on a module that requires some html to be entered to be later called upon and become part of a customer facing widget output.
I've created an administrative backend and that is all working properly, however when I enter html into the field that should be storing the data i receive an error.
I dont need the wysiwyg but I would like to be able to enter html into this value.
At this point I've not done anything special when adding the field to the fieldset. What am I missing?
$contentField = $fieldset->addField('inner_html', 'editor', array(
'name' => 'inner_html',
'style' => 'height:36em;width:36em',
'required' => false,
));
Try
$fieldset->addField('inner_html', 'editor', array(
'name' => 'inner_html',
'label' => Mage::helper('tag')->__('Description'),
'title' => Mage::helper('tag')->__('Description'),
'style' => 'width:700px; height:350px;',
'config' => Mage::getSingleton('cms/wysiwyg_config')->getConfig(array('add_variables' => false, 'add_widgets' => false,'files_browser_window_url'=>$this->getBaseUrl().'admin/cms_wysiwyg_images/index/')),
'wysiwyg' => true,
'required' => false,
));