I have a problem in CodeIgniter, and that is that when an image is not found on the server, the instance of a controller is created (besides the one that called the view).
I know all this can sound confusing, so this is the code to observe what I'm saying. I did this changes to a clean 2.1.0 CI version:
Add a controller to override the 404 error page, I added this one:
// add application/controllers/Errors.php
Class Errors extends CI_Controller {
public function error_404() {
echo 'error';
}
}
// change routes.php
$route['404_override'] = 'Errors/error_404';
Use a controller that isn’t the default one with an unexisting image, I used this:
// add application/controllers/Foo.php
Class Foo extends CI_Controller {
public function index() {
echo '<img src="doesntexist.png" />';
}
}
I couldn’t figure out another way of debugging it, so I created a log to write the events on CodeIgniter.php:
// add on CodeIgniter.php line 356
$path = 'log.txt'; //Place log where you can find it
$file = fopen($path, 'a');
fwrite($file, "Calling method {$class}/{$method} with request {$_SERVER['REQUEST_URI']}\r\n");
fclose($file);
With this, the log that generates visiting the index function is the following:
Calling method Foo/index with request /test/index.php/Foo
Calling method Errors/error_404 with request /test/index.php/doesntexist.png
Which is the problem I have, an instance of the Error class is created.
that is that when an image is not found on the server, the instance of a controller is created
Not really. What I believe is happening is that, since you're using a relative path for the image (and calling it directly inside a controller, which is wrong because you're ouputting something before headers), your browser attach the image directly to the CI url, thus making this request to the server:
index.php/doesntexist.png
Which is (correctly) interpreted by CI as a request to a controller, which doesn't exists, and therefore it issues the error class.
You could do, in your actual code (I'd put the images in a view, though):
echo '<img src="/doesntexist.png" />'
using an absoluth path, or using the base_url() method from the url helper:
echo '<img src="'.base_url().'doesntexist.png" />
This should tell the server to fetch the right request (/test/doesntexist.png) and won't trigger that error.
Related
I'm working on a new website. This website will be a one pager. All my files I already load in through PHP into the main folder. But now I want to edit them and update them through WYSIWYG.
The UPDATE and SELECT are already working. I tested it on a page who stood on its own. All the one-pager files are stored in a folder and within the folder is also the file which loads all the files into it. I call them through a href which ends up giving the file an # in the address bar. There lies the problem. I can't access the #file with the function I wrote because that only can access files without starting a #. Is it possible to access it through my function?
I give the code if the question is too unclear because it's a bit of mess to implement all the files I use for this purpose.
Short recap: Can't access #domain with a PHP function. Is it even possible to access it.
<?php
class Home extends Controller {
protected function frontpage() {
$viewmodel = new HomeModel();
$this->returnView($viewmodel->frontpage(), true);
}
}
?>
this is the returnView from Controller:
protected function returnView($viewmodel, $fullview){
$view = 'views/'. get_class($this). '/' . $this->action. '.php';
if($fullview){
require('views/main.php');
} else {
require($view);
}
}
I'm new with overflow so I couldn't get the function into right place but with protected function frontpage I should access the file frontpage.php. Well it does do that but that's not the right directory because it's only visible throug a href which means the function should have been: protected function #frontpage which isn't possible.
This is actually not possible, as the anchor (#) isn't sent to the server and is handled by the browser itself.
The only possibility is to include some JavaScript magic to your project.
<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.
Hi I am facing some problems in writing a cron job using CI CLI way. My application has a controller name called manager.php in that there is method called check_status where I am gonna get all the order_ids using one model function. Ever order_id row had a status filed in database which either success or failure.
I have an api if i pass order_id to that it will tell whether order is successfully delivered or not. But here comes the problem I have below line in controller in the top.
<?php if(! defined('BASEPATH') ) exit("NO Direct Script Access Allowed"); ?>
So when i try to run method check_status from CLI in CI it gives me an error stating NO Direct Script Access Allowed.
This is the way i called above method php application/controllers/manager.php check_status
So i decided like this i created an another class file called cron_job.php in that i didn't keep the above error line "No Direct Script Access Allowed". I thought it will give access now when i try to run but it doesn't give an error and even output also.
This is the class which i created and method in that.
<?php
class Cron_job extends CI_Controller {
public function message($to = 'World')
{
echo "Hello {$to}!".PHP_EOL;
}
}
?>
I run this controller form CLI like this php application/controller/cron_job.php message
Note: I am in ROOT directory.
No Output at all. So i tried in another way like this php index.php application/controller/cron_job.php message
Now it gives me error stating that Error 404 page not found.
What i tried in another way now i created a file in views folder and in that i am calling old controller/method url like below.
$result = file_get_contents("http://application_path/controller/method");
echo $result;
Now i am getting output which i defined in the method check_status in manager.php controller.
But here comes another problem now after the above line i will get an array output which had all the order_ids.
I am gonna send this each id to a api to check status. If it is failure it will check whether it is delivered or not. If it's done i need to update that status in the database against that order_id. But now i am in view file, is it possible to call a model file from the view file or is there any way to do this.
Any help?
Note: There is no syntax errors in any controller or any method , which are fully verified and working normally when i am accessing using urls.
You need to read the CodeIgniter help section on Running via the Command Line. It's very easy. Your original approach was correct. But you do not call your controller method directly by its path, instead CD to your project root and then the call the index.php file with the controller and method as parameters.
// This is how you call CI via the command line.
// Use spaces between index.php and your arguments.
$ php index.php <controller> <method> [params]
// And in your instance
$ php index.php manager check_status [param1 param2 param3]
Depending on your host you may need to call the PHP version compiled for CLI.
the ci helper will help you in this.
1 ) Create a helper & create a function in it, that calls your model function
function getUserDetails($userId = '') {
$CI = & get_instance();
$getUserDetailsByUserId = $CI->user_model->getUserDetailsByUserId($userId);
return $getUserDetailsByUserId;
}
2) Now you can call getUserDetails($userId); in your view.
I have an on-going CI v2.0.2 app that was coded by other developers.
I started off by creating a trial controller: `controllers/trial/trial.php'. The code in this controller is:
<h1>controller</h1>
<?php
class Trial extends CI_Controller {
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
}
And the view is in views/trial/trial_view.php. The view has a simple <p>this is the view</p> line.
Now when I visit the URL - http://localhost/ci/index.php/trial/trial all I get is the <h1> tag from the controller. If I remove that tag, nothing is seen, not even the echo statement.
The code base I was given is an exact replica of the app that is being used right now. I've doubly checked to make sure the folder structure is correct too.
What should be going on here? Any config options I should look at?
Update--------
I moved trial.php into the controllers folder, and trial_view.php into the views folder. Made the appropriate changes in the code too. But the result is still the same - only the h1 tag from the controller is displayed when I visit http://localhost/ci/index.php/trial
your action is called index, while you're trying to access controller's trial action, which does not exist.
change it to,
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
try putting trial.php outside trial folder inside controller folder, and get back what happens
try changing
function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
to
public function index() {
echo "this works";
$this->load->view("trial/trial_view");
}
BTW try turning on error reporting and see if error is thrown
EDIT
BTW i tested with your code with same setting. It is working in my machine
First off all, stop using the index method to do anything. If your class is called Trial, you need to do this with the index method:
public function index()
{
$this->trial();
}
Then do everything under a method called trial.
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.