Having problems with CakePHP 3 links due to shared hosting DocumentRoot - php

So, I've searched and found some simillar questions, but none of the answers worked for me.
Let me explain the situation. I've built a CakePHP 3 and it works great in my local machine. When I finally deployed to the server, things got really weird.
Basically, when I use the link() or css() or other methods of the Html Helper, CakePHP adds the whole path (starting from the root). So, I'd write something like:
<?= $this->Html->css( 'main.css' ) ?>
and the output should be:
<link rel="stylesheet" href="/css/main.css"/>
but what I'm getting is this:
<link rel="stylesheet" href="/var/www/html/mydomainname.net/web/css/main.css"/>
I did some research on the hosting company FAQs and documentation, and I found out that the Apache DocumentRoot is set to the '/', which is of course not desired, since CakePHP requires it to be set to the app's web root folder. The thing is, since this is a shared hosting environnment, I cannot change it!
Any ideas?

According to the docs:
This method of CSS inclusion assumes that the CSS file specified resides inside the /app/webroot/css directory if path doesn’t start with a ‘/’.
As a workaround then, you might be able to try:
<?php $this->Html->css('/css/main.css') ?>
to prevent cake from making assumptions.
From here: http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::css

Well, the solution I've found was simply to not use Html Helper methods such as link() and css(), and use plain good old html instead. The ideal would probably edit Apache settings and make DocumentRoot point to the webroot folder in my app, but my hosting company support was not as helpful as I'd wished them to be.

Related

Cakephp only loads one .css-file - 403-Error for all others

I have a problem which I find fairly strange, maybe someone can help me.
I use cakephp. My web root folder for apache is /home/jw/www. My project is under /home/jw/www/hc2, so the overall folder structure (only the parts important for this question) is:
www
/hc2
/app
/webroot
index.php
/css
/cake.generic.css
/any.css
/other.css
/stylesheets.css
/js
/any.js
/javascript.js
/files.js
Well, cakephp works fine so far - routing (with mod_rewrite enabled), database connection etc. works.
The one but huge problem is: No .css- or .js-files are loaded, except the cake.generic.css!
The part of the .cpt-template, where I load css and js files looks something like:
echo $this->Html->css('cake.generic');
echo $this->Html->css('bootstrap.min');
echo $this->Html->css('layout');
echo $this->Html->script('bootstrap.min');
echo $this->Html->script('jquery-2.0.3.min');
In the html- of the loaded page this becomes e.g.:
<link href="/hc2/css/cake.generic.css" type="text/css" rel="stylesheet">
<link href="/hc2/css/bootstrap.min.css" type="text/css" rel="stylesheet">
But when I examine those two with firebug, I see that the first of those is loaded correctly - I can see the css-definitions of the file - while the other one receives an error 403!
Is it maybe a mod_rewrite-problem? But since I can access all controllers just the way it should be, I can't quite imagine that... Does anyone maybe have an idea? Would be very happy, thanks...
Sry, I solved it myself - thanks to the help of #mvanderw.
It had nothing to do with cakephp/apache/mod_rewrite whatsoever. The problems were the File permissions. For some reason, I had copied all files except the cake.generic.css (which I downloaded) from my windows file system (on the same device) an for some reason, they were coming with the permissions 600.
chmodded to 644 now and everything works. So maybe this helps someone sometime in the future with a similar problem... :)

Installing Zend Gdata Client library

Hi this is my first post on here. I am trying to install the Gdata Zend Client library without much success.
I have used these resources + scoured Stack Overflow.
https://developers.google.com/gdata/articles/php_client_lib
http://jeromejaglale.com/doc/php/google_calendar_api
I want to be able to add,edit etc events on google calendar via PHP. My problem/question is i really dont understand what the include_path settings are all about and how to set them in order to make the class work. Of course i checked php manual regarding this but still draw a blank.
I have downloaded the relevant class and uploaded it to my web root. In the past i would just include a class by using php include at the top of the page and this would suffice.
I am of the understanding that i need to change the php.ini file to show php where my class is. Does this mean that i have to put my class somewhere else other than the web root.
I am terribly confused about this step and i know that if i can get it installed, actually using the class should be relatively easy.
Thanks for any help.
Welcome! Your Q is about include files rather than ZF elements.
Understanding where the include setting can be made (and subsequently overridden) is an absolute key bit of information.
You need to find out where it is on the server you are working on.
echo ini_get('include_path');
Then dash off and really, really read the corresponding manual page.
Try out including a very simple file with an echo statement, and you will regain your sanity and confidence.
http://www.php.net/manual/en/function.include.php
Get that working then have a play with this:
http://www.php.net/manual/en/function.ini-set.php
The experience how you can include a file from the same directory (not generally a good idea if that is a public webpage - inside your webroot)
Then if you want to really chase this thing down, look at where you can set this in Apache and per-directory in .htaccess files.
Finally, you can just include a file by telling include/require the exact path from the top of the tree;
include /var/www/includes/libraries/and/so/on.php;
There are SO many places you can set and override this that you are really best off finding out where the server thinks the include directory is and putting your compoenents in there:
Now, when that comes to ZF stuff, I (on Deb and Ubuntu anyhow) put the contents of
Zend Framworks version XYZ ZendFramework/lib/Zend <-that folder into:
/usr/share/php/Zend <-into this place
Then setup your autoloader and Robert is your mothers brother...
Zend/Gdata.php line 124 is like this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’)
Change that to this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’, $useObjectMapping = true)

Hosting piwik on a server with a read-only filesystem

I use CloudControl for hosting and I would like to set up a server (possibly with load balancing support) to host piwik for all of my websites. The only problem is that the only writable directory CloudControlled allows you to access is defined by $_SERVER['TMPDIR'].
Is it possible to modify piwik to use this directory for all of its file-writing needs?
And also will I run into any issues with using load balancing? Something like automatically generated reports being generated by each node behind my load balancer since they're not aware of each other?
The idea is to keep this change for your system even when you update.
This is easy to do: create a bootstrap.php inside the piwik main folder.
This is the content of said file:
<?php
define('PIWIK_USER_PATH', $_SERVER['TMPDIR']);
You can double-check this: in index.php, you should see that it checks for a bootstrap.php file in the same folder. It's included when available, and this allows you to do little customizations and keep them even when you update. E.g. I've run piwik from svn for the past three years or so and have some custom changes in there.
There's far too much code for me to be able to confirm this works, but the constant PIWIK_USER_PATH seems to be used as the base root for file io. With that in mind, editing index.php, around line 23, which is originally:
if(!defined('PIWIK_USER_PATH'))
{
define('PIWIK_USER_PATH', PIWIK_DOCUMENT_ROOT);
}
To something like:
if(!defined('PIWIK_USER_PATH'))
{
define('PIWIK_USER_PATH', $_SERVER['TMPDIR']);
}
Might work - but then what happens when it's trying to read a file in its original location? Since this is a temporary directory, however, it may not be viable, in which case an approach using override_function or a similar method, paired with a persistent storage (your database), might also work - by overriding file functions with a database load/save routine; obviously this opens up another can of worms of biblical proportions, thus, my final recommendation is for you to get another less restrictive host.

zend framework problem with paths

I have installed zend framework on my local machine. I have configured a vhost in httpd.conf and have added a line in my hosts file (127.0.0.1 mysite). I am running windows 7. Everything works perfect. The problem is when i upload on a hosting server the paths get mixed up.
I am uploading on a remote dir called zf-framework. To access the index page i need to type this url: http://mysite/zf-framework/public. It displays the index page but when i press any links on the page they get mixed up and end up being something like http://mysite/controller/action when in fact it should be http://mysite/zf-framework/public/controller/action. I have found a work-around for this situation...to use echo $this->baseUrl(link) for any links i have in the layout.phtml. The problem is more serious when it comes to submitting forms. I can't use baseUrl there....or i don't know how to use it. Is there a way to write some general config stuff so that this could be automatically resolved by the framework. Let's say to write something in index.php or bootstrap.php that will fix the paths automatically?
If you're using Zend_Application, then add the following to your configs/application.ini file.
resources.frontController.baseUrl = "/your-path-here"
If you're not using Zend_Application, then do this in your bootstrap, or index.php file.
$front = Zend_Controller_Front::getInstance();
$front->setBaseUrl('/your-path-here');
You won't have to use $this->baseUrl() when submitting a form to the same action and controller (just leave out the action attribute in the form tag), or when using the Redirector action helper. However, links in your view scripts will require you to $this->baseUrl('/url-without-base'), which doesn't seem too bad to me.
I am not 100% on this, but if you specify the route in your routes.ini as zf-framework/public/Controller/Action etc this should fix your issue.
I would see this as a bandaid, but I am not 100% sure on how to properly fix your issue other then you modifying the vhosts file on the remote server to set a document root to the public folder. If that is not an option, well the above should work, but know that all of your files are potentially accessible from everyone (at least your folder structure). I am not sure what harm this can do (if any) other then if your database schema is in the /data directory.
It is better to try and get the public set as the web root, if possible.

Missing Helper file error in cake php on shared hosting

I am getting a Missing Helper error when I am trying to upload my cake php files to a shared host.
Undefined variable: javascript
Missing Helper File
It is working fine on my local machine.
I have the following directory structure in shared hosting:(using cpanel)
/home/user/
/app
/cake
/vendors
/public _html
/css
/img
/js
.htaccess
index.php
I have also made all the relevant changes to the index.php file and I was getting the full colored cake php intro page.
The strange thing is that it is recognizing the HTML helper and hence i am able to see my web page with full CSS. However, it is giving problems with javascript and other helpers.
I already have an app_controller file with all helpers well defined in $helpers =array().
Why is it giving this error??
As mentioned above, I have made changes to my cake directory structure. So, do I have to make changes to the paths.php file located in the cake/config folder??
I think that cake is not finding the javascript helper because something is messed up with config files. Is it so or there is some other problem??
in /app/app_controller.php, add:
var $helpers = array('Javascript');
your structure doesn't seem like cakephp structure :-(
Guys, I found a simple way to tackle this problem. I certainly cannot call it the solution for the problem. May be a temporary way to make things work.
I simply replaced
<?php echo $javascript->link(array('tabulator')); ?>
with the standard HTML,
<script type="text/javascript" src="/blog/js/tabulator.js"></script>
and it worked!!
I still don't know the problem with javascript helper. All other helpers, Html, Form and Ajax are working fine. I am also using Gravatar for my blog. So, the Gravatar Helper was also giving problems.
The solution to this problem was weird.
I renamed the helper file gravatar.php instead of Gravatar.php. This worked insipte of the fact that helper file name should begin with a capital letter according to cake naming conventions!!
I also had to solve numerous other problems like making changes to the index.php file so that cake can locate the modified directory structure.
Also, had to make changes to .htaccess file for rewriting the base and directing the blog to the new URL.
Man..so much for rapid development!! :O

Categories