404 Error after Renaming Controller, View & Default Controller - php

I'm getting a 404 Page Not Found error in CodeIgniter\WAMP after doing the following:
renamed a controller (from welcome.php to search.php)
renamed the view to load from search.php as *search_page* (see below)
renamed the related view (from *welcome_message.php* to *search_page.php*)
renamed the default controller in config\routes.php to search
restarted WAMP
search.php
public function index()
{
$this->load->view('search_page');
}
config\routes.php
$route['default_controller'] = "search";
Where did I go wrong?

i'm not a codeigniter programmer, but you should make the class Search -> class Search extends CI_Controller, no? found it on this link -> http://ellislab.com/codeigniter/user-guide/general/controllers.html
i think it's the same in every framework, cakephp, codeigniter, zend etc.

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 3.0 custom 404 not found error page

My problem is about custom error pages after upgrading to Codeigniter 3.0.
I used to have a Errors controller which handled 404 error page of the website - on this controller I have customized some parts of page, such as if user is logged in, it was showing username on the navigation bar and etc. (yes, on 404 error page).
After upgrading to CI 3.0, I have noticed that error pages are handled in different folder, I started to migrate to this way; but the problem is I can't load any variables to this page, such as session variables or can't even use any CI's functions on these pages.
I think this pages are supposed to be only HTML pages, but is there any way to load variables to these error pages?
You need to set in application/config/routes.php the following route
$route['404_override'] = 'my404';
Then you need to create a new controller in your controllers directory (application/controllers/)
<?php
class My404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$this->load->view('err404');//loading in custom error view
}
}
And create an err404 view. Thats all!
Refer :Reserved Routes
It's not working in codeigniter 3, I overwrite the default view (application/views/errors/html/error_404.php) and added my custom view, now show_404() is working fine.
that is working good, but if redirect to an error, for example 404 with function show_404, your controller wont load your view, for a real 404 in the show_404 method, you have to go to the core of codeigniter and touch it.
\system\core\Common.php
this is an example of code:
function show_404(){
$ci = get_instance();
$ci->output->set_status_header(404);
$ci->load->view('errors/error404_view');
echo $ci->output->get_output();
exit(4);
}
In Codeigniter 3, instead of calling show_404(), I just redirected to the custom error controller function.
e.g. for the above scenario
if (404 condition...) {
redirect("my404");
}
To work with the show_404 () function, you don't need to access the CI core and change how they are talking.
Just make the standard 404 view load your 404 view with $CI->load->view(...)
this will keep the original 404 code of the request and will not make an unnecessary redirect or core changes.
$CI =& get_instance();
$CI->load->view('error-404');

codeigniter routing not working for single view page also

I have single controller and one view page when i try to load this view page using controller I am getting 404 error even route has set.
Controller:
class Navigation extends CI_Controller{
public function index(){
$this->load->view('header');
}
}
In the view directory i had place a file with the name of header and it contains as "Test Navigation" in body that's it.
Routing file:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['menu'] = 'Navigation';
This CI script placed under directory of books. So when i type as localhost/books/index.php/menu , I am getting 404 error. Please explain how to solve this.
Controller class name & controller filename should be same. class Navigation should be under Navigation.php.
Note: This rules applied for the Model too.

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";

CakePHP plugin throws "Missing View" error but view file exits

I'm writing a basic plugin for my cakePHP 2.x app following the instructions in the book.
I've created the directory/file structure with a MyPluginAppController.php and MyPluginAppModel.php.
I added CakePlugin::load('MyPlugin'); to the parent app's bootstrap.php file.
Then I created one Controller and Model. But for some reason when I try to view mysite.dev/(admin)/my_plugin/my_model/ I get a "Missing View" error. It says to confirm that the view file exists, which it does!
I don't think I skipped any steps from the book. What am I doing wrong?
Update:
Controller path: app/Plugin/MyPlugin/Controller/MyModelController.php
View path: app/Plugin/MyPlugin/View/MyModel/admin_index.php
URL: http://mysite.dev/admin/my_plugin/my_model/
Rename this file
app/Plugin/MyPlugin/View/MyModel/admin_index.php
To this extension .ctp
app/Plugin/MyPlugin/View/MyModel/admin_index.ctp
This is a common mistake.
I was't using a plugin so my case was a bit different. However, this question shows up on the search results page, so I'll post this here.
This is the problematic controller.
<?php
class ArticlesController extends AppController {
public function beforeFilter() {
}
public function view() {
// do stuff
}
}
The problem:
CakePHP kept showing me the following error:
Missing View
Error: The view for ArticlesController::view() was not found.
Confirm you have created the file: Articles/view.ctp in one of the following paths:
Debugging:
I checked all paths it suggested using is_readable() and the stat command to make sure the view file exists and is indeed readable by PHP.
Solution:
The issue there is the empty beforeFilter(). As soon as I removed it everything worked.

Categories