Relative paths for javascript on CakePHP2 - cakephp

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.

Related

CakePHP passing root dir to javascript

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']); ?>',

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.

Call a Angular js page in Play framework

I am finding trouble to display my AngularJs page from Play framework 2.2
The Angular js page is located in the same project directory which is
C:\webProj\test\app\www\index.html
Note this index.html is not the index.scala.html that we have in play views directory
I need to render this page from my Play project.
I have tried this
GET /masterid controller.Assets.at("/app/www/",index.html)
but it gives a compilation error.
Explanation of the error
There are some syntax errors in this route configuration:
GET /masterid controller.Assets.at("/app/www/",index.html)
It should be controllers instead of controller, the path is wrong, and the parameters are not specified correctly. It could be written as:
GET /masterid controllers.Assets.at(path="/app/www", file="index.html")
Note that as written, this route will map only to the index.html file, not to any other resources under /masterid.
Solution with a separate directory
To behave exactly as asked, with a separate directory and a custom URL, you would need to specity a second asset route in addition to the default one. This would also require changing all usages of #routes.Assets.at to specify two parameters (folder and file), and adding a configuration to build.sbt:
playAssetsDirectories <+= baseDirectory / "app/www"
Solution using the public directory and a custom URL
The path of least resistance is to create the custom index.html file in the project's public directory. To use a custom URL as asked in the question, you could change the default asset path to "masterid" by changing this line in the routes file:
GET /assets/*file controllers.Assets.at(path="/public", file)
to this:
GET /masterid/*file controllers.Assets.at(path="/public", file)
In this case the custom index.html file could be accessed as:
http://localhost:9000/masterid/index.html
Relative URLs to other resources under the /public folder would work as well.
Solution using the public directory and the default URL
If you don't require the /masterid URL under the root, you can save your index.html file under public/app and refer to it as:
#routes.Assets.at("app/index.html")
This will resolve to:
http://localhost:9000/assets/app/index.html
Documentation
For more extensive instructions see Working with public assets.
Make it faster, just place your file in i.e.: /public/angular-app/index.html, so you can use it via:
#routes.Assets.at("angular-app/index.html")
Next (assuming that you have standard routes) you can just use static paths to your public assets, i.e. if image is placed in folder public/img/logo.png you can access it with:
<img src="/assets/img/logo.png" alt=""/>
So just by replacing public/ to /assets/ (slash at beginning to make sure you don't need to use base tag in head of document).

How do I define an environment depending on what the current URL looks like?

I come from the server, and in the Laravel framework, we have a concept of 'environments'.
There's a paths.php file, and you can define an array like so:
$environments = [
'local' => ['*.dev', 'http://localhost/*'],
'staging' => ['staging.url.com']
]
Corresponding config files can then be made inside subfolders 'local' and 'staging' and stuff like database settings, etc., is pulled from there.
Why do I want this?
I'd like to auto detect the base URL to use depending on where I am:
angular
.module('myApp.services', [])
.value('homeURL', 'http://local.dev')
//.value('homeURL', 'http://server.com');
Then I can proceed to use {{ homeURL }} in various views of the app etc., Right now, I comment/uncomment the appropriate lines just before pushing to server, but would be nice if there was an easier way...
I found out that I could use angular's $location.host() instead, which fits my needs perfectly.

Cakephp multilanguage support in Javascript

How to give language strings in js while using cakephp (Im using cakephp version 2) ? I know, this can be done in the following way.
<script>
var LABEL_LOGIN = '<?php echo __('Login'); ?>';
</script>
I want to separate my js code from my view file. Anything wrong if I do the same in .js file (because I am using global vars) ? Is there any other good solution to apply multilanguage in js ?
Cake does not support i18n in JS out of the box. I would rather recommend pulling in some i18n JS plugin which is syntactically compatible, i.e. also uses a global __ function.
Then you coul use the same method to parse all source codes for i18n keys.
The only easier way i could think of is:
Create some element, say langs.ctp, and add your global js vars in there, like
<script type="text/javascript">
var LABEL_LOGIN = '<?php echo __('Login'); ?>';
var LABEL_LOGOUT = '<?php echo __('Logout'); ?>'; // and so on
</script>
and then load this element in your layout, like, in your layout inside head tag
<?php echo $this->element("langs");
then you could use your global js vars
Hope it helps

Resources