Is this Codeigniter code correct? - php

I've just started looking at PHP Frameworks after spending loads of wasted time doing everything from scratch. I thought I would give Codeigniter a go after a friend recommended it to me.
I worked through the first tutorial and there were no issues but I'm getting stuck with the second one.
At first my code was identical to the tutorial:
<?php
class Blog extends CI_Controller{
function Blog()
{
parent::CI_Controller();
$this->load->scaffolding('entries');
}
public function index(){
$data['title'] = "My Blog Title";
$data['heading'] = "An intresting title";
$data['todo'] = array('create media player','design site','finish project');
$this->load->view('blog_view',$data);
}
}
?>
But I got an internal server error. After looking at the user guide it shows a different way for using the constructor.
I changed the code to the document spec.
<?php
class Blog extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->scaffolding('entries');
}
public function index(){
$data['title'] = "My Blog Title";
$data['heading'] = "An intresting title";
$data['todo'] = array('create media player','design site','finish project');
$this->load->view('blog_view',$data);
}
}
?>
But I still get an internal error. I don't know if my code is wrong or I misconfigured something else. Some advice would be good, thanks.
Edit:
To clarify - I only get an internal error when I add the constructor to the class.

This will most likely because you're trying to load scaffolding in the latest version of CodeIgniter.
Scaffolding has been depreciated since version 1.6. It was removed in 2.0, and the latest version is now 2.0.2
In terms of which of your provided snippets to use, the latter form of declaring a constructor (__construct) is preferred over using the Class name (Blog). This method is also consistent with the Core Classes of CodeIgniter since 2.0.
As an aside, I've got no idea why CodeIgniter are including such out-of-date samples in their tutorials. EllisLab's seem to have lost total interest in the CI project recently, which is a shame :(

Internal error means there's mis-server configuration.
Usually .htaccess file or apache settings. Try this:
Enable rewrite module in apache.
Make sure .htaccess file is configured correctly. It should look like this: https://gist.github.com/ce2914e79193896590bc
This also removes index.php, which you should look into.

Related

Error when loading library even library loaded

I'm now updating my company's old CI2 Website to CI3. Model, Controller and View from the CI2 seem to work fine after a little bit changes but my app keep showing "Fatal error: Class 'CI_Func' not found in pathtoapp\system\core\Common.php on line 196" when load library, the "Func.php" with "F" is capitalization, it's location is in "pathtoapp\application\libraries" and just echo "Hello world" for testing purpose. The text "Hello world" showing up but the app still showing the error. I've tried many other libraries but it just showing the same kind of error like "Fatal error: Class 'CI_Someotherlibrary'...". My system is running using IIS, PHP 5.5 and MySQL.
Here is my Func.php and my controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Func {
function __construct()
{
echo 'Hello world';
}
}
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('url', 'form', 'download', 'text', 'views'));
$this->load->library('pagination');
$this->load->Model("Menus");
$this->load->Model("2019/News","News");
$this->load->library('Func');
}}
I don't know why it keep adding "CI_" before my library name. I'm still new with CI and new to stackoverflow too. I've already search over internet and try almost everything but still no luck. Any advice will be appreciated. Thanks in advance.
PS: Sorry for my bad English.
update infomation from #Bira quest
- When i change the name of class to CI_Func or $this->load->library('func');, the new error coming up: "An Error Was Encountered Unable to load the requested class: Func"
Update 1:
Here is two threads that nearly like my issue but can't solve my problem
Thread 1
Thread 2
Update 2:
- I figured it out that when i'm not using $this->load->view in my Views, the error disappear. May be the cause is $this->load->view trigger something that make loader class went wrong. I just don't know what wrong with it because i'm using $this->load->view to load some part of my website layout and it worked fine with CI2. Someone help me, please.
Update 3:
- I've solved it. In my view, i create new instance of the controller using like $home = new home(); so may be it load the library again and cause the error. Thank you all for your helps.
I've solved it. In my view, i create new instance of the controller using like $home = new home(); so may be it load the library again and cause the error. It's hard to trace the error. Thank you all for your helps.

Ion auth Codeigniter Login throwing errors after logging in

This is my first project using both Codeigniter and Ion Auth and I've finished this tutorial: http://www.rappasoft.com/tutorials/view/5#.U1bJLeZdWpo
But now that I've reached the end, I have an error that is appearing when all of my code looks identical to the tutorial. The error is:
Message: Creating default object from empty value
Filename: core/MY_Controller.php
Line Number: 13
But the odd thing is that the code from the MY_Controller file is copied right from the tutorial. Here's the code I have:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->ion_auth->is_admin()) {
redirect('auth/login');
} else {
//Store user in $data
$data->user_info = $this->ion_auth->user()->row();
//Load $the_user in all views
$this->load->vars($data);
}
}
}
?>
The correct view loads underneath the error but obviously I'd like to know what's wrong. Since this is my first turn time working with ion-auth and I'm relatively new to code igniter as well, I was wondering if anyone knew how to go about debugging an error like this. Teach a man to fish, feed him for a lifetime! I'd really appreciate not just solutions but methods so I can learn and do this on my own.
Are you working on PHP 5.4+ ?
In that case, you must declare $data before using it:
Change:
$data->user_info = $this->ion_auth->user()->row();
to
$data = new stdClass();
$data->user_info = $this->ion_auth->user()->row();
and try again. Hpe it helps!
I think I might have figured it out. I think the issue might be that the default login information (the ones provided by ion auth) do not have any additional information in the database. This means that when the $data variable goes to set the additional account information, it doesn't find anything. It's not a strong enough error to crash the page but it is warning me that there is no other account information available even though it is supposed to be looking for more info.
So how to fix this? Create a user for testing purposes with a full compliment of account information and see if that solves the problem. The only problem is that I don't know how to add new users using ion auth and my views aren't sophisticated enough yet to make that process easy.
I've looked at the create_user() method in the auth.php file but when you press submit after filling out the form, it kicks you back to the same page with all the info. I have a feeling an edit needs to be done here although I don't quite know what to change. I've been looking on the ion auth documentation but it seems a bit... sparse. I think I'm on the right track although I'd really like to hear if someone else has a better explanation or solution. As of now, I haven't actually fixed it yet. Just deduction.

codeigniter+HMVC cross module call controller->method

I am pulling all my hair off... Have been searching every thread, would appreciate if someone can point me to a working example.
Accroding to the doc: https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
I can call another module->controller using
modules::run('module/controller/method', $params);
modules::load('module/controller/method', $params);
or
$this->load->module('module/controller');
$this->controller->method();
Problem: the "method()" is never called. only constructor of the controller is called every time.
The objective is to build self-contained MVCs as module and use by other controllers.
But no matter what I do, it only calls the constructor, method is not called.
I started using HMVC a few weeks ago, did I miss something in the doc or it is not used this way?
Here is the setup:
modules
|--ztest1
| |--controller/c1.php
|--ztest2
|--controller/c2.php
class C1 extends MX_Controller {
function __construct() {
parent::__construct();
}
function index () {
Modules::run('ztest2/c2/testc2/');
//Modules::load('ztest2/c2/testc2/');
//$this->load->module('ztest2/c2/testc2/');
//$this->c2->testc2();
}
}
class C2 extends MX_Controller {
function __construct() {
parent::__construct();
echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
}
function testc2(){
echo __FILE__." // ".__CLASS__."/".__FUNCTION__.PHP_EOL;
}
}
output:
/app/modules/ztest2/controllers/c2.php // C2/__construct
additional note: no error or warning with the script. It just quietly calls the constructor.
Thanks for MC's tip, I finally figured out the cause. HMVC doc indeed lacks some examples for beginner.
For anyone who may find this thread in the future, correct usage here:
to call module01/controller01/method00:
//method 1 CORRECT:
$ctlObj = modules::load('module01/controller01/');
$ctlObj->method00();
//or you could use chaining:
modules::load('module01/controller01/')->method00();
//method 1 WRONG:
modules::load('module01/controller01/method00'); //this will only load contructor
---
//method 2 CORRECT:
modules::run('module01/controller01/method00'); //no trailing slash!
//method 2 WRONG:
modules::run('module01/controller01/method00/');
---
//method 3 CORRECT:
$this->load->module('module01/controller01');
$this->controller01->method00();
I don't understand why method 3 failed when I first try... maybe because I restarted HTTPD?
This HMVC works well for me. I'm working on a project using this HMVC now.
Just edit third_party/MX/Modules.php as shown in this link below and tell me the response.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/pull-request/5/return-error-messages-instead-of-logging/diff
I ran into the same issue. Make sure you check capitalization of your directories and and controllers. It's not case sensitive for differing between the module and controller name.
//In my case the below did not work
$this->load->module('dashboard/Dashboard');
$this->Dashboard->method();
//but
$this->load->module('dashboard');
$this->Dashboard->method();
//worked
After some attempts to achieve call a controller that is not located within any module.
Modules::run('../Controller/method');
I am new at CI as well, and I thought I was having the same issue. Script seemed not to be running. (no html output).
//This did NOT work (did not produce output)
modules::run('module_name/method_name',$data);
// but this DID work??? didn't know why
modules::run('module_name/method_name',$data);
exit();
// turns out you need the echo for output
echo modules::run('templates/login_template',$data);
This may be obvious to many of you- but I wasted two hours searching for an answer.
so According to the documentation they says copy the controller in default controller folder and move to modules controller.
So now how do I run the controller that has been moved to modules when I run its running from the default controller file if removed does not work so how to make it run the controller inside module as a default controller to run.
So Do I need to mention the modules name too in the route
/*echo Modules::run("controller name of a module which you want to call/and its.. function name");*/
echo Modules::run("Second/callit");
or
$this->load->module('Second');
$this->second->callit();
But.. the controller name shold be different .. from one module to another module..
**(parameter passing)**
echo "<hr>";
//echo Modules::run("controller name of a module which you want to call/and its.. function name");
$data="peter";
echo Modules::run("Second/callit",$data);
echo "<hr>";
$this->load->module('Second');
$this->second->callit($data);
echo "<hr>";

CodeIgniter: Can't Get My New Controller/View To Show

I am learning how to use codeIgniter as my php framework. I am reading through the documentation and watching the intro video and just generally following along with the first tutorial, but it's not working for me.
I have created a controller called "test.php" and a view called "test_view". The controller for this class is exactly like "welcome.php" and the view file just has some static html. However, when I go to index.php/test I get a 404 error.
I have also tried manipulating the original welcome files so that instead of calling a view it just echos "testing", yet I still see the original welcome message! I've tried clearing my browsing cash and refreshing, but to no avail.
Any suggestions? Thanks.
Edit: Here's the code for controllers/test.php
<?php
class Test extends Controller {
//Just trying to get it to echo test
public function index()
{
echo "test";
//$this->load->view('test_view');
}
}
?>
Try looking at this page in the documentation - this might solve your problem.
This basically means you should try typing index.php?/test/ instead (notice the question-mark).
First of all, check the above link. Might be useful.
If not, then...
Try changing the default controller in the config file ('routes.php') to something else (probably, to 'test'), then try loading index.php. Just to test whether the whole system works (or not).
Check whether mod_rewrite is loaded (in your server .conf-file, if you're using Apache).
Try using the latest build of the framework. AFAIK, the name of the controller class for now is "CI_Controller".
Finally, try removing the word 'public' before the declaration of the function. AFAIR, CI enable you to make private functions in controllers just by using prefix (which is set in the config file) at the beginning of the name of the function (thus making all the other functions public).
But most certainly the problem is with the mod_rewrite. If not, try debugging with die('Page found'); instead of echo - this will allow you to track possible redirects on the page.

CakePHP Facebook integration tutorial "Fatal error: Call to a member function share() on a non-object" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call to a member function on a non-object
I'm doing this tutorial here:
http://tv.cakephp.org/video/webtechnick/2011/01/12/nick_baker_--_facebook_integration_with_cakephp
I baked a new project with cake bake facebook_app and set the configuration database file to the correct settings and the default cakePHP screen showed that tmp directory was writable, DB setup was good, etc.
I downloaded the CakePHP plugin by WebTechNick here: https://github.com/webtechnick/CakePHP-Facebook-Plugin, and filled out app information (app_secret, app_id, etc), adding it to facebook_app/config/facebook.php
Changed facebook_app/app_controller.php:
class AppController extends Controller {
var $name = 'Facebook';
var $helpers = array('Session', 'Facebook.Facebook');
}
Then just exactly as in the tutorial `facebook_app/views/pages/home.ctp':
<h1>Facebook_App</h1>
<?php $this->Facebook->share(); ?>
returning the error message:
Undefined property: View::$Facebook
I realize that means PHP didn't recognize Facebook as an object. But I installed the plugin!
Also, it seems not MVCish to have something like $this->Facebook->share(); in a view (home.ctp). However, this is exactly how WebTechNick does it in his tutorial (I followed it exactly 3x) and it does not work for me. I'm a complete noob at cakePHP (although I've read the entire documentation) and I'm just trying to learn and understand through examples.
:) To be fair, it's PHP - you didn't install anything. Or if you prefer, "install" != "invoke." PHP is really amazingly easy to debug. I mean, it tells you exactly what's wrong:
Like turning to a channel that's not on the air, the error your getting means that the object you're calling doesn't actually exist, at least not in the scope you're trying to invoke it.
Is that your IDE? Is it set up for your Cake app? Are you sure the instructions were to set your AppController's $name to 'Facebook' instead of $name = Facebook_App in your AppController? It looks like you either replaced your actual app's AppController with the plugin files instead of putting them in the proper directory, or the plugin is not deferring / calling / extending / returning to the application the way it's supposed to. Knee jerk -> typo, naming conflict, path problem, permissions.
Cake's not even rendering. I can tell because your screenshot would show that error with the styled Cake errors. That tells you it's erroring before AppController class makes it to View class.
Create an instance of the Facebook object statically in the view and see what happens. Then, what does
function beforeFilter() {
parent::__construct() ?
}
get you? Anything? What about debug(), var_dump, the object functions will also shed light on what's happening. So will your logfiles.
Btw, if you don't use them already: Firefox + FirePHP + Xdebug = made of win.
I was having this problem and I found that the plugin I was using was for CakePHP 1.3 and I was using Cake 2.0. I found the BETA branch for the upgraded Cake 2.0 and it worked perfect.
Here is the Cake 2.0 BETA Branch

Categories