2

I am wondering where to put my JS files so I don't make a complete mess out of my folders.

If I do a search on "Where to put Js files in cakephp" I get results of people explaining how to include a Js file into a page.

This JS file uses Ajax to post for example, books, to the database. So do I place this file in the controller folder, do I place it in Webroot/js/ under a certain folder or do I place it in the view folder?

deltu100
  • 581
  • 7
  • 26

2 Answers2

1

If you can't even answer that question for yourself, I highly recommend you to read the manual. If you already struggle with where to put the JS, you definitly will have trouble to figure out everything else as well.

The webroot directory is the public document root of your application. It contains all the files you want to be publically reachable.

JS goes to /webroot/js, images to /webroot/img and make your guess where the CSS goes to.

You should also read about the HtmlHelper to display files from the webroot.

But I personally would start here and do the tutorials.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • I understand, but a specific function that does a post to a database seems so odd to place in a "general" js folder in the webroot. I will read the link, thanks! – deltu100 Jul 08 '15 at 12:14
  • 1
    It's still client side code that doesn't belongs into the /src folder. If you want to write proper organized JS google for "js namespace", check this http://stackoverflow.com/questions/881515/how-do-i-declare-a-namespace-in-javascript or use a JS framework like Angular or others to structure your Js code. In theory you can use a single giant JS file but for development this doesn't make any sense. So usually you use multiple JS files and a build script to built a single JS file that is then moved in the right place. – floriank Jul 08 '15 at 12:18
  • You JS file probably posts to a PHP which in turn makes the changes in the database. In your PHP file, the first thing to check before executing any code would be to validate if the user is logged in. – Ahs N Jul 08 '15 at 12:19
  • Thanks @burzum together with the comment my answer is fully answerred. – deltu100 Jul 08 '15 at 12:24
  • The first paragraph really wasn't necessary. Not everyone has God-given skills like you. – Peter VARGA Aug 07 '20 at 19:07
  • Reading the official documentation is a god like skill in 2020? Yea, the standards get lower and lower every year, I really should accept that... – floriank Aug 07 '20 at 19:19
0

you should put your js file inside webroot/js folder. and then you can simply include the js with the function in the view.

echo $this->Html->script('/js/fileupload/jquery.ui.widget.js');
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68