CSS and JavaScript Files Not Linking in CakePHP Website - database

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.

Related

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

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!

How to bake admin (prefixed) code with CakePHP 3?

Can anyone tell me what is the official way to create CRUD for admin back-end?
In CakePHP 2 the baked code was extended with 'admin_' before the function names and for the view files.
In CakePHP it seems I can't find any direct information on how it's done anymore. The bake console doesn't ask for admin anymore.
In this topic: https://github.com/cakephp/bake/issues/28 I see that they mention to use the --prefix extension but then the controller is placed in a separate /Admin folder while the CRUD functions keep having their normal name. And in some parts of the cookbook () I still see they mention functions like admin_view.
So can anyone tell me what is the official 'Cake'-way to do this from 3.2 on?
If you want to create Controller using cake bake. You can do this with below command:
bin/cake bake controller --prefix admin users
For view:
bin/cake bake template --prefix admin users
It creates the admin folder in the template directory, Then it creates the folder for users, then it includes the files. for admin prefix folder structure like template/admin/users/index.ctp
See official cookbook documentation
Also In your config/routes.php add this:
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->fallbacks('DashedRoute');
});
That's how things work now in CakePHP 3, prefixed methods are gone, prefixes now do generate separate controllers in sub-namespaces, for smaller/simpler controllers, and for proper separation, not only on controller level, but also on template level, where the templates are expected to be placed in separate folders accordingly.
The admin_view example you are referring to is just an example that should show how to manually set a custom layout for specific actions, it has nothing to do with prefix routing.
So, if you want to use prefix routing, then the "official" way is to use the --prefix option.
See also
Cookbook > Routing > Prefix Routing
Cookbook > Bake Console > Code Generation with Bake
Below is the bake command to bake all prefix controller and templates for users table
cake bake all users --prefix admin
And here is the route code to make it working:-
Router::prefix('admin', function ($routes) {
// Because you are in the admin scope,
// you do not need to include the /admin prefix
// or the admin route element.
$routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
$routes->extensions(['json', 'xml']);
// All routes here will be prefixed with `/admin`
//$routes->connect('/admin', ['controller' => 'Order', 'action' => 'index']); // call other controller like this
// And have the prefix => admin route element added.
$routes->fallbacks('DashedRoute');
});
This will work for me hope it will work for you :)

cakePHP without domain name

I've set up my Raspberry Pi 2 with a nginx, php, mysql environment and installed cakePHP with composer.
Now the cakePHP landing page looks weird. I'm assuming that this issue is related to my nginx vhost configuration. (Conf with vhost works, CSS files loading)
The question is: is it possible to use cake without vhost conf?
The route is set to the respective folder:
$routes->connect('/test/', ['controller' => 'Pages', 'action' => 'display', 'home']);
With best regards,
Phil
Solved the problem by adding these lines into my /etc/nginx/sites-enabled/default conf-file:
location /test/ {
alias /usr/share/nginx/www/test/webroot/;
}

How to set up a website's URL properly

I'm having trouble to decide how I should do to make my websites urls cleaner. The thing is, it's done with Angular AND Symfony2. Angular providing the front, and Symfony2 providing the api.
Here's my website's structure :
/api
/app => configurations go here
/bin => binaries
/src => your bundles/MVC code
/vendor => Symfony and 3rd party bundles
/web => this is where the web server document root should be pointed to
/front
/api -> Symlink to /api/web in the symfony part, working, but with app.php and app_dev.php
/css -> frontend CSS
/img -> frontend Images
/js -> frontend JS for AngularJS
/lib
/partials -> html templates used by AngularJS
index.html
Now the urls I get are www.project.dev/ which is home, rendered by AngularJS (/front), works perfectly, and www.project.dev/api/app(_dev).php/... rendered in Symfony2 but I want to remove the part :
app(_dev).php
I have two empty .htaccess, in / and in /api, do I need to use them ? Or do I have to use the Angular-route system ?
How am I suppose to do it ?
It's not clear how exactly you have your Symfony application setup, but I would recommend using the default Symfony directory structure, as it will simplify troubleshooting problems. The default directory structure is something like this:
/Symfony
/app => configurations go here
/bin => binaries
/src => your bundles/MVC code
/vendor => Symfony and 3rd party bundles
/web => this is where the web server document root should be pointed to
Since your document root is pointed to /web, you can have directories in there for your public assets, such as your /js, /img, and /css (Or you can look into Assetic, it allows lots of cool things like combining and compressing js or css.). The /web directory also has a .htaccess file included so that the app(_dev).php is removed from the URL.
Your /api can then be built using the standard Symfony routing, controllers, and views. You should read the Symfony routing documentation to get a grasp of how that works. A summary of how this works is:
create the route (ex. /api/users)
point the route to a Controller action (see the docs)
in the controller action, return the desired response
That's how I would recommend you structure your application. But if you would rather stick with the structure that you have, you will need to add a .htaccess file to the /api directory (which I assume points to the /web directory of the Symfony application), and take a look at this post for what the .htaccess file could contain.

URL rewriting is not properly configured on your server

I tried upgrading my cakephp version from 1.3 to 2.5 but encountered an url rewriting problem. Another cakephp app runs on the same version so I'm sure the mod_rewrite works.
Although when I go to example.com/pages/home I get following error:
URL rewriting is not properly configured on your server. 1) Help me configure it 2) I don't / can't use URL rewriting
When I go to my normal home page and I want to click on a home link I get following url :
http://example.com/app/webroot/index.php/
And it is the same for other urls.
http://example.com/antwerp becomes http://example.com/app/webroot/index.php/antwerp
When I go to http://example.com/antwerp the site works as it should...
Any ideas where I've gone wrong?
I would suggest accessing your account and re-uploading your .htaccess files from and to the following locations respectivelly.
/app
.htaccess
/webroot
.htaccess
.htaccess
Some FTPs do not upload those files automatically until you force it to.
If that does not work, check if you have your php.ini file in your public_html folder of this application.

Resources