CakePHP passing root dir to javascript - cakephp

I am not quite sure how to achieve passing the relative project root directory to a javascript file.
Purpose of having this is to call an ajax function that should use relative paths to the root dir.
However, one ugly method would be to store the dir in the meta fields of the HTML header, but I really want to avoid this.
Any theoretical suggestion?
Are there any kind of Javascript helper in version 3.0?

You can use router class for that
Use below namespace
use Cake\Routing\Router;
Then in your ajax URL you can use below code
url:'<?php echo Router::url(['controller' => 'YourController', 'action' => 'Method']); ?>',

Related

App::build in CakePHP 3

Firstly, I'm very new in cakephp. In version 2.x, it allowed App::build to specify Controllers in specified folder. For example:
App::build(array(
'Controller' => array(
ROOT . '/app/Controller/Api/'
)
));
But in cakephp3.x, App::build is no more available. So how can I make a same thing in cakephp3.x ?
As written in the cakephp doc here : http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#configuration, the App::build is not a part of cakephp3 anymore.
So, you'll have to make a specific configuration for the cakephp autoloader (use composer):
"autoload": {
"psr-4": {
"App\\Controller\\": "/path/to/directory/with/controller/folders"
}
}
More information about this config : http://book.cakephp.org/3.0/en/development/configuration.html#additional-class-paths
More information about composers autoloader :
https://getcomposer.org/doc/01-basic-usage.md#autoloading
App::build has been removed but what you want can be done with prefix routing in Cake3. This is exactly what you try to solve. Taken from the documentation:
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having prefixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Using our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src/Template/Admin/Users/edit.ctp
Just replace admin with api from the example and read the whole section of the manual I've linked and you're done.

Relative paths for javascript on CakePHP2

I was wondering if there's any way to do something similar to what HtmlHelper does in Cake views: instead of having to write the URL manually, calling some kind of helper to make it for you.
I have taken a look at this post but it is from 2010 and maybe there's something new now...
Currently I have codes like this at my javascript files:
window.location.href = 'http://' + document.domain +'/cakephp/posts/view/'+$(this).attr('data-id');
But if i change the cakephp folder name, or I use another configuration on the server or something similar, the URL changes and I should change manually all the codes with this type of URL.
I wonder if there's something similar to:
echo $this->Html->link('controller' => 'users' , 'action' => 'login');
Are you referring to inline or external JavaScript? Inline is rather easy using the url method, which accepts the same arguments as link (an array or string containing the URL).
Using it in external files is a bit trickier. I define a JS variable containing the site path before loading the JS files.
<script type="text/javascript">
//<![CDATA[
var SITE_URL = "http://www.example.com/cakeapp/";
//]]>
</script>
In my external JavaScript files I can then reference SITE_URL when I need it. When you change domains or rename the cakeapp directory you only have to alter the SITE_URL variable.

Load files that aren't within the Elements or View folders?

Hi I'm trying to make something like CMS with widgets so I have a folder widgets in my app folder there are my widgets for example app/Widgets/UsersOnline/UserOnline.php
I loading them from db with widgets model in beforefilter method in appController and there i pass them to the view, so with a widget helper I would like to render them on the position. But i can't get to the folder widgets/UserOnline/view.ctp with view->element() method this method require file to be in Elements/.
TLDR / Actual Question:
Is there any way to load files in view outside the view/ and /elements ? Thanks in advance.
You can use relative paths when calling the element:
<?php echo $this->Element('../../Widgets/UsersOnline/UserOnline'); ?>
Don't forget to name your element file 'UserOnline.ctp' too.

How do I add an script to all actions of a controller?

I have an script file called 'game.js' which I want to add to all actions inside game_controller.php.
I think there is a better solution than copy and paste "Html->script('game', array('inline' => false)); ?>" inside all my ctp files. How do I do that?
I suggest that you create a sperate layout for given pages and put the script in the layout. If you simply MUST do this, then you can use the beforeRender() method like so:
cakephp: can I set $scripts_for_layout from within a controller?

Route to absolute URL CakePHP 2.0

I want to access my sitemap.xml file in /mywebsite/sitemaps/xml/sitemap.xml
Reading some documentation, it is explained that I can reach the absolute URL by using:
Router::url('/',true)
For some reason it doesn't work. How to solve this?
If your site map xml is generated outside of CakePHP, then you can create the folders and file in /mywebsite/app/webroot/sitemaps/xml/sitemap.xml and avoid routing altogether.
If you are dynamically generating the xml through cake, then you don't want to hard-code the route. Rather, you would route to the controller and action that provide the xml return. A very basic example would be..
Router::connect(
'/sitemaps/xml/sitemap.xml',
array(
'controller' => 'Xml',
'action' => 'sitemap'
)
);
The XML documentation here can help you get started building and routing the site map.
http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html

Resources