![]() |
I have a bug for days which drives me crazy. I spent hours to try to solve it but I could not. It's really annoying because I guess it only takes 2 minutes to solve it when you know CodeIgniter :(
In my menu.php View, I want to add : <script src="menu.js"></script> which is in my View folder too, but it did not work. I get a 404 Not Found error. In Config, my App.php is well set for public string $baseURL I tried to use $baseURL in my url too, but always the same error. Finally I tried to put the file in the public folder and : <?php helper('html'); echo script_tag('public/menu.js'); ?> But always the same 404 Not Found error. I tried autorouting, and a lot of other things, but nothing worked and now I am juste crazy because adding a simple .js file should be easy :( Do you have any idea to help me ? Thanks a lot
It seems it is not a CodeIgniter thing, but just a HTML thing.
See https://www.coffeecup.com/help/articles/...athslinks/
You cannot use links from the app/* folder, it is not available for the browser. Need to move *.js to public/ and paste the url to the file <?= base_url("script.js") ?> in tag <script>
You should keep all your resources in a folder called assets in the public folder.
PHP Code: <!-- JavaScropt here for Faster Page Loading. --> What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks all for your answers.
@kenjis : no, I use HTML for more than 20 years, I know what is an absolute or relative link, thanks. @InsiteFX : I've found that everything is redirected to the "public" folder, I guess it's the default configuration in the main htaccess. RewriteEngine On RewriteCond %{REQUEST_URI} !^/public/ RewriteRule ^(.*)$ public/$1 [L] So I tried <script src="menu.js"></script> and it worked finally !! The menu.js should be in the "public" folder. Note that <script src="<?= base_URL('menu.js'); ?>"></script> doesn't work : error 404 not found (which is logical because menu.js is not in the root of my project) and <script src="<?= base_URL('public/menu.js'); ?>"></script> doesn't work either : error 404, the same that I got, which is frustrating because the full link is OK but error 404. Exactly the same for <script src="<?= base_url()."public/menu.js" ?>"></script>, same (theoretically good) link, same error. Another time, in Config, my App.php is well set for "public string $baseURL" which is of course important. So I will finally create a js folder in my public folder, and use <script src="js/menu.js"></script>. I guess that my .js will be public but I guess that all js are always public because loaded by the client browser, isn't it ? Coding is so frustrating sometimes... Thanks everyone.
I handled it this way
in my view file: Code: <script> Views/js/form_js.php is really Javascript but starts with php tags enclosing a comment Code: <?php The rest is just straight JavaScript. There is no other PHP code and no <script> tags. In other words I took what would have gone inside <script></script> in the view, put it in a .php file. (08-11-2023, 07:30 AM)arna Wrote: I've found that everything is redirected to the "public" folder, I guess it's the default configuration in the main htaccess. There is no .htaccess in the project root folder by default. So if you have it, someone added it. And it is the last resort and not recommend. See https://codeigniter4.github.io/CodeIgnit...g-htaccess So if you can configure to point the document root to the public folder, you should do so.
Really ? So I guess I did. But if I remove it, my website totally disappears and the page shows an automatic page from my privider, saying that my website is not online.
Why not recommended? Do you have any idea? I'm struggling so much with Routes, thats absolutely crazy. This evening I lost more than 1 hour only with Routes problems when I tried to add an Ajax request on my website. Each time I spend countless time looking for explainations and solutions in the CodeIgniter online help guide, in this forum, in google, asking tons of questions to chatGPT. Most of the time in vain. I have to say that sometimes it's so irritating and frustrating that I'm considering stopping completely this whole CodeIgniter configuration. Thanks.
If file saved as /public/js/menu.js and baseUrl equal http://domain.com/
<?= base_URL('js/menu.js'); ?> return http://domain.com/js/menu.js This is a working option. If you get the wrong address in the source code, it means an error in the configuration. Your problem does not mean that the framework is bad, but just ignorance of its work. In 20 years, you could look at what is output to the source code and find a solution |
Welcome Guest, Not a member yet? Register Sign In |