CakePHP3/twig - How to clear view's cache? - cakephp

I'm using CakePHP3.6 and Twig template engine.
However, when I deploy our production, it dose not reflect.
Probably I think that cash is working.
I ran this command: bin/cake cache clear_all.
However twig's cache didn't clear.
(cache path is tmp/cache/twigView/)
How can I delete this cache?
I don't want to run the command at production's server: rm -Rf *...
Please help me.
Thank you.

If clearing the CakePHP cache won't affect Twigs cached files, then whatever you're doing to integrate Twig, it's not using CakePHP for caching.
Depending on how you integrate Twig into your application, there might be separate tools to clear the cache, but if that's not the case, then you'll either have to go with either deleting things via rm (there's nothing wrong with doing that, especially if you're using a hardcoded path... you can look into using something like safe-rm if you're worried), or you could try to setup a dummy cache configuration with CakePHP that points to the twigView path, something like:
'twig_view' => [
'className' => 'File',
'path' => CACHE . 'twigView' . DS,
'prefix' => '',
],
That way you should be able clear it using the cache shell, either by using clear_all, or by targeting that very specific configuration:
bin/cake cache clear twig_view
Note that CakePHPs file cache engine will not remove directories, it will only remove the files inside of them!

Related

Wagtail admin won't load most css or js static files

I have a site https://resilium.group that I recently upgraded to Wagtail 4.2 and added a generic admin view using ModelViewset to.
Now, if I visit /admin, some of the css and js fails to load, whereas it did before and I don't know why.
Some observations:
All 404 errors happen inside wagtailadmin
Some wagtailadmin js will load. These are all in the vendor or images subfolder
Some of the hashed versions will not load, while the unhashed will load:
e.g. this will not load: https://resilium.group/static/wagtailadmin/css/core.7ffe08725eaf.css
This will load: https://resilium.group/static/wagtailadmin/css/core.css
The same goes for core.js, vendor.js, wagtailadmin.js, telepath.js, sidebar.js
And sometimes both will not load:
This will not load: https://resilium.group/static/wagtailadmin/js/vendor/jquery-ui-1.13.2.min.26d3af3a7ec4.js
This will also not load: https://resilium.group/static/wagtailadmin/js/vendor/jquery-ui-1.13.2.min.js
Same for icons.js
At this point, I struggle to even know where to look. I'm also having difficulty getting the issue reproduced locally. Everything works ok if I'm serving local static files. I appreciate any help you can give.
If this is a production site (i.e. not running with DEBUG = True), you need to run ./manage.py collectstatic. This step is necessary after any deployment that changes static files, including Wagtail upgrades.
It looks like Cloudflare is caching your 404s. Try purging the entire site:
https://developers.cloudflare.com/cache/how-to/purge-cache/#purge-everything

react + webpack - pass POST data to build

Coming from a PHP background, I used to have an index.php which does two things:
serve the webpage if no parameters were set;
or serve JSON data when a specific POST parameter was included in the request.
Something like this:
// -- index.php
<?php
if ($_POST["some_parameter"]) {
...
echo json_encode(someArrayData);
exit(0);
}
?>
<html>
...
</html>
I have built the complete frontend application with npm, webpack, webpack-dev-server, and react. Having completed the first part, how can I effectively serve JSON data instead of HTML when a request includes a specific POST parameter?
I can see 2 ways of doing this:
Build the frontend as usual and everytime I build the bundle, modify index.html, inject my PHP code in it, and rename it to index.php. I then would have to run this folder via apache or nginx, so I'd be able to run the index.php script. This method is downright ugly and is probably the worst way to do it.
Run a separate PHP server which just serves data or redirects to the static webpack-generated build. All requests should then start from this server, and this server determines whether to serve data or redirect to the frontend. The problem comes to neatly passing the POST data received from the request to the static react app. As far as I know, the only way to do this would be to include a URL (GET) parameter to the redirect and manually parse it with javascript on the frontend. This is a dirty solution, in my opinion.
So, to summarize:
I need an efficient way to get POST data in a react/webpack/webpack-dev-server environment.
It should work with my hot-module-replacement dev setup.
I'm fine with switching to a node-based backend like express.
There shouldn't be any ajax involved in the static react app.
Any ideas? There has to be a way to do this properly.
UPDATE: I solved this by simply copying an index.php from my source directory to my build directory via the webpack config. I serve the build folder to a PHP server and keep a webpack --watch building my source.
I lose built-in features like auto-reload and css injection, but it's worth the convenience of not having to implement SSR for a very simple task (getting a single POST variable).
For anyone interested, I also added 2 npm scripts:
npm run start runs my original webpack-dev-server with hot-reload, serving static content including a static index.html file
npm run static runs the webpack --watch which copies the index.php file to the build directory
This lets me have hot-reloading when developing frontend, and allows POST data fetching when programming logic.
It's easy, convenient, and works on most web hosting providers.

CSS and JavaScript Files Not Linking in CakePHP Website

I'm running my test sites off my local host using MAMP on PHP v5.2.4. Ive made quite a few attempts to take backups of two websites configured in CakePHP v1.2, along with transferring the databases and putting in the correct credentials for the database. How the websites connect to the database is through the database.php file in the app/config folder, putting in the credentials below:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysqli',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'nameofdatabase',
'prefix' => '',
);
}
I had the database configurations put in correctly after inputting the username and password.
On the homepage, it looks like the following:
But, when I click on one of the links, it looks like the normal page, reading in the CSS and JavaScript files out of the webroot folder.
I tried following #EoinMurphy's answer and writing in the RewriteBase / code on all of the .htaccess files with no luck. Even though I have the wrong version of CakePHP, I still used these instructions on configuring the website because it had clearer instructions then this set of instructions that confused me.
I don't understand why it seems the CSS and JavaScript is being read on all of the internal pages, but not the homepage of the website? The only way I can figure out how to link the CSS and JavaScript files is using the <link src="" ... /> on the file to output the index page content.
The only other earlier issue was the output of the links like (http://...) after the links, but it was simply the following in the CSS that make it output:
a:link:after, a:visited:after {
content:" (" attr(href) ")"; /*the content CSS made it spill out the links*/
font-size:90%;
}
This is relating to my latest findings with moving CakePHP websites from a live environment to a development environment.
When CakePHP is put into a sub-folder of the webroot, the Apache config, RewriteBase must be used.
So if you've installed WAMP, enabled the MOD_REWRITE extension, and moved the 'live' website into C:\wamp\www\live
Open the .htaccess files in \live.htaccess, \live\app.htaccess, \live\app\webroot.htaccess
and add the line 'RewriteBase \live'.
As seen here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
Just to recap a few things to remember:
Enable mod_rewrite
RewriteBase if in a subfolder
Ensure database base settings are CORRECT.

CakePHP 1.3.0 RC4 Installation

I have been using 1.2.6 and downloaded 1.3 to try it out. I am using wampserver with Apache 2.2.11. I have mod rewrite enabled. When I bake a new application using the cake 1.3 console, after I verify that the webroot/index.php of the application CAKE_CORE_INCLUDE_PATH is set to the Cake1.3 Installation folder. The result is a page thats missing CSS, missing a method. Typing in localhost/app/index.php will enable the program to find the app. Therefore I have to assume the rewrite module isnt functioning properly for v1.3. Is there something I have to add to the cake installation or app folder to make this function as it should?
No, Cake is set up properly from the start. The thing that always trips me up is that I forget to set my Apache virtual host's AllowOverride value to All. This allows the .htaccess file to be read.
Are there any error messages in your apache error logs?
Did you replace the previous application's directory with the new one? If not, you may need to add a tag with the appropriate AllowOverride permissions set inside your httpd.conf file, to allow mod rewrite to execute.
Can you load /css/style.css in your browser or does it give a 404?
This may sound like a silly question, but after you made your changes to AllowOverride in your Apache configuration, did you restart Apache?

CakePHP home page - cache not working

In my CakePHP home page, I get the message:
Your cache is NOT working. Please check the settings in APP/config/core.php
I googled for possible solutions, some of which were to uncomment the Cache.check and Cache.disable line. Even then the error isn't resolved.
Also the webserver runs on www-data (exec('whoami');) whereas my home folder is public_html. How do I change www-data to public_html?
Besides, check if you have the original directory structure inside /app/tmp/cache.
Recently I had the same problem, and found out that for some reason my /app/tmp was empty. Redoing the directory structure helped.
Assuming your cache is set to file, make sure your /app/tmp and all the folders inside are world-writable (drwxrwxrwx).

Resources