Codeigniter Custom Controller Receiving 404 - php

So I copied the controller file Welcome.php and named it home.php.
I changed the class name from Welcome to Home.
When I go to view it in http://daytodata.net/ci/index.php/home I get a 404?

Did you try accessing your method using /home/index/ ?
Also you should consider take advantage from routing system:
https://www.codeigniter.com/userguide3/general/routing.html

Related

Codeigniter controller method 404 error

I cloned a copy of Codeigniter from github today using this link: https://github.com/bcit-ci/CodeIgniter.git. I am currently running MAMP setup so that http://localhost:8888 points to my htdocs folder. My root folder is called 'time'. When I go to http://localhost:8888/time/index.php, I do see the Codeigniter welcome page. Also, I can go tohttp://localhost:8888/time/ and see the same welcome page, even though I don't have an .htaccess file in the root directory.
Here is the problem. I added the following function to the Welcome.php controller class:
public function test()
{
echo 'Test';
}
This should display a page which shows 'test' when I visit http://localhost:8888/time/index.php/test. However, I get a 404 page not found error. Does anyone have any suggestions for understanding and fixing this problem?
Because localhost/index.php/test doesn't refer to the method test in the welcome controller. You would have to go to localhost/index.php/welcome/test or use routes.
They way you are doing it implies there is a controller named Test.php and it is trying to go to the index() function of that controller.

codeigniter issue with loading view from controller

I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";

Easy way to find the controller file with only the URL on Cake PHP

Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}

CodeIgniter: Not able to hit controller specific method

I am new to Codeigniter. While trying to access controller specific method, I am getting 'Object Not found' exception.
Before hitting the url I did following changes:
base_url set as http://localhost/test/ in config.php
default_controller set as main
Defined index and login method in main.php class
If I try to hit just http://localhost/test/, it returns echo from the index method. But if I directly give http://localhost/test/main/login then it throws Object not found exception. Strangely if I give $this->login(); into index method of main controller class, http://localhost/test/hits the login method. I have tried to change login method to public but no luck.
What I am missing here?
It seems to be the common index.php problem
This should do it: http://ellislab.com/codeigniter/user-guide/general/urls.html
But please confirm it by trying to access the controller with a index.php in behind it like so:
http://localhost/test/index.php/main/login

Default Controller in CodeIgniter

I am wondering if there is any other configuration options for a default controller.
For example - if I have a controller called "site" and I set the default controller in the following file: application/config/routes.php to:
$route['default_controller'] = "site";
I should be able to go to http://localhost and that brings up the
index(); function in the site controller.
However, if I try to do go to http://localhost/index.php/index2 to load the index2(); function I get a 404 error. If I change the URL to http://localhost/index.php/site/index2 it works fine - but I thought already set the default controller. Is there any way around this?
The only way to do it is manually writing the routing rule:
$route['index2'] = "site/index2";
You cannot do that.
The default_controller is only for URLs without any URI parameter. How could CodeIgniter distinguish between a method name and a controller name?
What you can do:
define a redirect inside your 404 document or directly map your 404 document to index.php
No CI doesn't work like that the first parameter has to be the name of a controller, so in this case you would have to create a controlled called "index2".

Categories