Why does index.ctp render as text and not as html/php? - php

I tried researching this question on my own for awhile, but can't seem to pinpoint the solution. I'm working through the CakePHP Blog Tutorial, and at this section: http://book.cakephp.org/view/1536/Creating-Post-Views
I create the index.ctp file with the code given by CakePHP. Then, when I open the index.ctp file in Safari, it simply shows the code given by CakePHP. It doesn't not render any HTML, nor does it pull the blog data from the database.
If I switch the extension from .ctp to .php, it renders HTML but it does not pull blog data from the database. My database is connected and has the correct information. I think there is an issue with the .ctp extension? Not sure. Has anyone else run into this kind of issue?

First, try this url:
http://localhost/posts/index
Second, you may have not mod_rewrite enabled in your development environment.
On ubuntu for example you can enable it with like this:
sudo a2enmod rewrite
(EDIT: restart apache afterwards)
Third:
Check your .htaccess, you need mod_rewrite to be enabled. Also check the tutorial/cakePHP documentation to find out if you missed any step concerning .htaccess and the Rewrite Rules.

RedEye,
call it the way like tacone wrote
make sure the file is in the correct path according to the conventions
make sure your server process has correct access rights for the file under concern

Please make sure the AllowOverride is set to All (instead of None) in your sites-available/default file.

Related

Viewing a PHP after uploading on Filezilla

This is my first time I use Filezilla and ftp. I uploaded my files.php. After I entered the hostname on address bar, I could see which files I had uploaded.
The interesting part comes here: When I clicked on index.php to view, It displayed only the html parts.
When I opened page source code, I saw that my php was commented, like this:
From what I had originally written:
<?php include '/includes/php/menu.php'; ?>
it became:
<!--?php include '/includes/php/menu.php'; ?-->
If anyone could help me I would appreciate it. Does any of the above has anything to do with host properties? From what I read, it supported php.
Your host may support php, but that does not mean that PHP is enabled. You can perform a very simple test to find out.
Create a file named test.php and drop one line of php in it and save it:
<?php phpinfo(); ?>
Upload the test.php file to the server and point your browser to it. It should output a bunch of information about the web server if PHP is working.
If not, check your host admin (cpanel) to see if you can enable it yourself. If you don't find it, just submit a ticket to have your host enable it.
PS: Remember to delete the test.php from your site as it is not great for security to leave it there.
UPDATE:
It sounds like you don't have Includes module installed. If you are using Apache, make sure the Apache Module mod_include. To check if you have the module installed you should create a file called phpinfo.php and upload to your server root and it should include the following code:
<?php
phpinfo();
?>
Then load phpinfo.php in your browser and mod_include should be included in the section Loaded Modules. It would be highly unusual if it wasn't there, as it is the default to have it installed on most Apache systems. If you are not on an Apache server, you can still follow the above instructions.
You can set the default editor to view/edit files under Settings->File Settings->Filetype Associations. An example using Notepad is below. There is also a box to inherit system filetype associations and I have it checked.
txt C:\Windows\System32\notepad.exe
php C:\Windows\System32\notepad.exe
I wanna thank all for your support. I am rather newbie in PHP and with you advice I have learnt a lot in the process. I have low rep so I can't give good rates to your answers.
The problem consisted in ftp, but later after changing some options in the host everything went OK. I guess working with PHP offline differs from online.
Thanks a lot everyone

I'm having issues stopping Apache from rewriting URLs, even after a restart

All I want to do is be able to see a PHP file from the root without it going through the rewrites (thus our custom CMS).
Here's what I've done:
In my httpd.conf, I've commented out the call to load the mod_rewrite module.
Next, I've removed the .htaccess file from the root as it had the rewrite rules in it.
I've rebooted the server.
I've searched through phpinfo() and there are no "rewrite" strings to be found
The problem is that the server still rewrites and the request passes through our CMS.
I'm using XAMP (PHP 3.3.1, Apache 2.2.14)
What am I missing?
maybe cache problem in your browser? because a similar thing happened to me and deleting the cache solved it.
Leave it to the details... I was trying to access my_file.php on the root. However, the filename was actually my-file.php (dash vs underscore).
The page I was getting was a 404, which went through the CMS. When I access the proper file, the one with the dash, I get what I'm supposed to.
I appologize for the waste of time :)

How to detect if mod_rewrite is available?

Good day. I have a problem with links in my application. I want to use 'pretty-urls',so i want to all my links look like 'www.sitename.com/controller/method'. So i want to print my all my links through one function,which would look like
function link($url_segments) {
if(mod_rewrite_is_available)
return BASE_URL.$url_segments;
else
return BASE_URL."index.php".$url_segments;
}
But i can't understand how to detect if server has rewrite module enabled. I have found 2 solutions:
Place in config some variable(like codeigniter does) like
$config["index_variable"] to use inside my function,but that means
that all my clients will need to change that line in config when
installing application on hosting
Use function to detect mod_rewrite,like apache_get_modules,but like
i understood it can be not avaibale on some hosting,and what if
server will be nginx?
So i want to know,how to detect that and print my links.
Thanks everyone for help.
Can you just provide the .htaccess to your clients so that, when you upload the files, it turns the rewrite mod on?
This can be very tricky and hard, personally, I would go with the fallback approach.
Look at apache2handler's loaded modules (via phpinfo()) to see if mod_rewrite is there
Try sudo apache2ctl -t -D DUMP_MODULES to see if mod_rewrite is loaded
Try to use a .htaccess file to see if everything is working.
Try to determine the server software used, and parse the conf file for the server
Use worst case of no mod_rewrite is enabled, and then enable it manually after the software has been installed.

PHP scripts are not executing on some of the pages

One of my php website is not working fully. Its showing me only home page and when i try to open up any present link it shows me the php code of the file that is called in for that link.
PHP is installed properly. Same server is hosting more than 10 other websites of PHP. No .htaccess parameters etc.. Same site is working on some other server with same settings and same code... I am unable to find the reason..
Make sure you are using
<?php .. ?>
tags everywhere, you might know this, but I am pointing it out just in case.
In earlier versions, apache would pick up and compile anything with
<? ... ?>
tags and it was a mistake since it was getting confused with the xml file versions.
so they changed it to complie only stuff in
<?php ... ?>
Also, again, you might know this, make sure you are hosting it on a php supported server.
There is also this "feature" that you can configure in your .htaccess to ignore certain files. make sure your .htaccess is not having exceptions for php compilation.
I cant think of anything else right now :)
Make sure everything of your PHP-Code is between the php tags like
iamserious has written upstairs.
Make sure you're filename's extension is .php index.php for example.
Make sure it's in the path to the file is ok!
Make sure you're webserver is working perfectly with PHP!

How to run CakePHP app from subdirectory

Using CakePHP 1.3, I have the following layout:
/srv/www/_cakephp13/
/app/
/cake/
/plugins/
/vendors/
etc...
/srv/www/htdocs/
/cake_app/
/other-dir/
... where htdocs is the public directory, and I have moved webroot of my app to be cake_app above... Which appears to be almost working. (After customizing webroot's index.php).
When I access http://devserver/cake_app/, I am able to see the default "Home" page controller/view (which I have customized, so that appears to be working).
But when I access http://devserver/cake_app/controller/view I am receiving 404 errors.
Is is possible to run a Cake App in a sub-directory of a site root? How can I get this working?
Ideally, I don't want to edit Apache conf's... If I can fix this with Cake configuration or htaccess files, I'll be much happier.
You should verify that .htaccess is being run and that the rewrite module is on and working.
You will need to have mod_rewrite enabled on Apache. To check if it's on, make a page that just does this:
echo phpinfo():
Grep on the text "loaded modules". Make sure mod_rewrite is listed there. If it's not, you can either add it to apache's configuration, or you may need to install it (depending on your original installation method and whether you included that module).
If you can't get mod_rewrite running, you can still use cake without it (the URL will just look like index.php/controller/action/etc). Check out http://book.cakephp.org/view/333/A-Note-on-mod_rewrite for more info. Specifically,
If you don't want or can't get
mod_rewrite (or some other compatible
module) up and running on your server,
you'll need to use Cake's built in
pretty URLs. In /app/config/core.php,
uncomment the line that looks like:
Configure::write('App.baseUrl',
env('SCRIPT_NAME'));

Categories