0

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?

1 Answer 1

1

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']); ?>',
Sign up to request clarification or add additional context in comments.

2 Comments

This is only possible if the javascript is inside your view or not? Otherwise, how should it be possible to use PHP code inside your javascript? For me that sounds like a security risk
You would put something like this, presumably wrapped in <script> tags, in the view, or the layout if you needed it everywhere. Any other JavaScript that you have would then be able to reference the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.