Fatal error: Cannot redeclare (PHP/Apache Setup) - apache2

I am running an Apache2/PHP5 web server on my Mac (Mountain Lion). When I upgraded from Lion to Mountain Lion, I lost my dev environment/configs. Whilst trying to set everything up, I have somehow borked my httpd or php.ini configuration (I think, anyway)... when I point my browser to my localhost, I get the infamous PHP error:
Fatal error: Cannot redeclare (some random function).
Regardless of the page I point it to, I get this. Prior to the loss of my web server settings, this was not happening, so I am confident that the files are okay and all syntax is good (the whole site consistently uses include_once and require_once also).
I think it has to do with my virtual host setup or working directory setup... I've tried a number of things, but no joy so far.
I am happy to provide any/all info that would be useful... I'm at my wit's end on this. Any help or suggestions would be greatly appreciated.

You've most likely changed your include_path so you are loading the same php file using different paths. When you do this, php can't tell it's the same file, so it loads the file a second time, which causes the Cannot redeclare error.
One way to track down the problem is to add the following right before the offending line:
echo "<pre>";
print_r(explode(PATH_SEPARATOR, get_include_path()));
print_r(get_included_files());

Fatal error: Cannot redeclare (some random function)/class
Always Means that the function or class with the SAME name has been defined before,
Take a look at this example:
<?php
function test(){
return 1;
}
function test(){
return 2;
}
//will produce fatal error like yours
class A {}
class A {}
//Will say that it can't redeclare class A
Here's the clue you should start from
Make sure that:, your include path is set accordingly
To get some clue, try:
<?php
print_r ( get_included_files() );
?>

In my particular case, it happened to be a bad vhosts setup. Once I fixed a kind of redirect issue there, it solved the problem.
Both of the other answers (and comments) supplied here are VERY good though, and should help anyone with similar issues in the future debug their code. Very rarely will you goof up like I did with vhosts and run into this problem (but not a ton of others). Big thank you to Ross Smith II, metal_fan and Jeffrey for their help in this!

I've just fixed that problem looking in another forum.
In my case, te problem was that:
<?php
include "file.inc";
$user = new user()
....
?>
<?php
include "file.inc"
....
?>
I included two times "file.inc", and that was the problem. Juust keep the include in the "main" file and that's all!

Related

In CakePHP, can someone explain how __d() works, particularly in relation to how it is used in default.ctp?

Sorry this will be a super newbie question.
In the default.ctp layout file, towards the beginning there are two lines.
$cakeDescription = __d('cake_dev', 'CakePHP: the rapid development php framework');
$cakeVersion = __d('cake_dev', 'CakePHP %s', Configure::version())
I've found the function for __d here which says it "Allows you to override the current domain for a single message lookup." I don't really understand what it means at all.
Additionally, I was able to do the same thing without throwing any errors by simply replacing the lines with
$cakeDescription = 'CakePHP: the rapid development php framework';
$cakeVersion = 'CakePHP ' . Configure::version();
Its used for making your application multilingual.
You can use the command Console/cake i18n extract to extract all the text strings(which is the second parameter in __d()) into a pot file, which you can then translate.
See this for more information about __d() https://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html#internationalizing-cakephp-plugins
And see this for more information about extracting the strings into pot files
https://book.cakephp.org/2.0/en/console-and-shells/i18n-shell.html

Error on Laravel 4 production: Call to undefined method Illuminate\Database\QueryException::message()

I'm managed to set up a L4 project on to a sub directory:
http://secure.myultratrust.com/test/users/create#b
I have it on a subdomain too:
http://test3-l4.ultratrust.com/users/create#b
I get this error on submitting:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Call to undefined method Illuminate\Database\QueryException::message()
(BTW, ignore the #b at the end of the URL. I'm using a backbutton javascript functionality called backb). =)
Both have the exact same message. I have it completely working on my local environment. I thought it may be the database settings so I tested the database on another test file (not connected to the l4 app) to see if the db settings were corret. The db connections work but again the test connect file isn't using l4.
The l4 route seems to be working for, at least, getting to blade views to show. However, the form submissions aren't working. I was trying to get my PHPStorm to step through but I can't get it to work right now so I find it's hard to track down this problem. =(
So what could be the problem? Any help is much appreciated!
Thanks and have a blessed day<><,
Victor
Replace message() with getMessage(). So your line 116 on UsersController.php should read like this:
$error = $e->getMessage();
This should fix the particular error. However since the PDException is being thrown, it should cause another exception which will then show to the screen to enable further debugging.
How did I figure this out? the $e variable is an instance of PDOException. If you look at the public API for this class, you find that there is no method called message() you can call. However this is getMessage() and that's probably what you meant to do in the first place. Access the public API here:
http://php.net/manual/en/class.pdoexception.php

how do i see cakephp database save errors?

if i have a cake php saveAll method like so:
if ($this->Video->saveAll($this->data)){
... // stuff that never happens, sadly
} else {
...
$this->Session->setFlash('boo! hss! error here');
}
how do i print out the database error? I tried:
$this->Session->setFlash('boo! hss! error here' . print_r($this->Video->validationErrors,true);
but that didn't work (it just showed me an empty array)
cheerio!
UPDATE:
ah. So, the problem is that, while normally i'd get the database error, i was using the old prg mechanism, and cake doesn't (magically) show the db errors on redirect pages.
Fair enough, but in the future, how the heck am i meant to see the db errors on a redirect page (that is, the question still stands, its just that most people probably just SEE the error, and don't need to do anything to get it)
make sure debug is set to 2 in config/core.php
print error messages to the log file like so:
$this->log(print_r($this->Video->validationErrors, true));

Controller Redirect issue in CakePHP

I have a controller with name users_controller, within the login action I want to redirect to my affiliate_redirect_controller.php, now I the following code in the users controller to redirect
$this->redirect(array(
'controller'=>'affiliate_redirect',
'action'=>'logRedirect' ));
And then I get the following error which I can't seem to resolve
Error: The requested address '/affiliate_redirect/logRedirect' was not found on this server.
I honestly do not know what this could be, quite new to cakePHP and none of the solutions found work for me.
the contents of affiliate_redirect_controller.php looks like this
class AffiliateRedirectController extends AppController
{
var $name = 'AffiliateRedirect';
function logRedirect(){
}
}
I can see there is a mistake in your code its because of naming convention.
$this->redirect(array(
'controller'=>'affiliate_redirects',
'action'=>'logRedirect' ));
Please see the above changes when you are writing your controller name in lowercase like above it should be plural affiliate_redirects and should not be affiliate_redirect
Apart from this you can use directly redirect as like this also.
$this->redirect('affiliate_redirects/logRedirect');
Please try, it should work.
Do you have a table in your database that corresponds to affiliate redirect controller?
You might want to rethink your logic, and use CakePHP routes to set the URL to what you want. Having a controller named affiliate_redirect_controller doesn't follow CakePHP's naming conventions.
Since I don't know exactly what you're trying to do, I don't know if this will work for you, but maybe consider redirecting to a separate action in UsersController like /users/affiliate_redirect/
Or you can create an AffiliatesController and then redirect to /affiliates/redirect/
Also, if you don't have debug mode set to 2, you should do that. It may help reveal what the actual issue is.
What debug level do you have in app/config/core.php ? Most of the time, when you get the message
Error: The requested address '/controller/action' was not found on this server.
it means you have a debug level set to 0 and increasing it to 1 or 2 allows to get more details about the error.

CakePHP Routing Alias, no prefix

I have a dashboard with a series of widgets. Per specification, the widgets need to be buried under a /widgets/ directory.
So I have added the following to my routes.php
Router::connect('/widget/:controller/:action/*', array());
But I seem to be running into trouble on widget/links/ and widget/links/view/1
I am new to CakePHP, but this doesn't seem all that impressive. I have yet to find anything in the Book or by search. So any help is appreciated.
Thanks.
Well...at the risk of stating the obvious...your route starts with /widget/, but you indicate that you're trying to access it via a plural URI (/widgets/). That's a problem. If that's just a typo, it would help to know what error you're seeing when you "run into trouble".
UPDATE:
Yes that was a typo. I corrected it. The error that appears for widget/links/ is: Error: WidgetController could not be found. It appears my index/default route is the main problem.
Given that information, it appears that CakePHP thinks that widget is your controller. Cake processes routes top down and finds the first one that matches. Ensure that you don't have a route above this one that looks something like /:controller/... or any other route above this one that starts with a variable.

Resources