CodeIgniter 4.0 shows me 404 error repeatedly - php

I wrote the corresponding controller through the CodeIgniter document, but the page always shows me 404. My question is why exactly the same URL can not be displayed, and why this framework can only show me 404 pages. I remember that ThinkPHP has a complete running link for error reporting, and I can't see the problem with only one 404.
this is my url
http://localhost/index.php/demo/test
this is my Demo.php code
`<?php
use CodeIgniter\Controller;
class Demo extends Controller
{
public function test()
{
echo 1;
}
}`
Demo. PHP files must be in the controller folder and there are no other directories, but CI reports 404 errors to me

Its because they dramatically changed routes, you need to define in \Config\Routes.php and check their wiki as I am in process of refactoring my scripts.

Add a route with this
$routes->get('pineapple','Demo::test');
ensure your server has mod_rewrite enabled and that you have .htacces file.
make sure that your base_url in app/Config/App.php is set with an ending slash on the url /

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.

Backbone and Codeigniter - Routing problems

I have the same problem like its asked here Routing problems with Codeigniter and Backbone.js - so, is there an existing example how this is solved? How and where do I have to adjust my code? And how do I dump Codeigniters View part and let Backbone do the complete MV part like it is suggested?
Thanks in advance...
[UPDATE]
So, after looking at Catch-all Controller/Route, in my application->config->routes.php Do I have to add:
$route['(:any)'] = 'catchall_controller'
additionally to $route['default_controller'] = "main" or instead of that?
then in my main Controller I have to do this or what?:
class Main extends CI_Controller {
public function index()
{
$this->uri->segment(n);
$this->load->view('home');
}
}
Finally remove the pushState:true in my Backbone App so it says:
Backbone.history.start({root: App.ROOT });
is this correct?
Your problem comes from the fact you're using pushState. From the documentation (http://backbonejs.org/#History):
Note that using real URLs requires your web server to be able to
correctly render those pages, so back-end changes are required as
well. For example, if you have a route of /documents/100, your web
server must be able to serve that page, if the browser visits that URL
directly.
So your options are basically:
stop using pushState Backbone.history.start({ root: App.ROOT });
make your server return a valid response for every URL your Backbone app can generate (see
Catch-all Controller/Route)
You're running into issues because your client-side applications is generating URLs that your serever doesn't know how to process => 404 error
See also Backbone router: Use hashbangs

Incorrect routing via /config/routes.php

I have the following (basic) route set up in a CI-based web app:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
The controller + 'resend' method:
class Sms extends CI_Controller {
public function resend($to, $message) {
// my code
}
}
Logically speaking, anything that doesn't fit the route should be directed to a 404 page instead of the resend() method within the sms controller. This isn't the case, however. The following URL, for example, isn't redirected correctly, it goes to the same controller+method:
http://myapp/sms/resend/uuuu/WhateverMessage
What could be the problem?
After a bit of digging, I've come to understand that CI's default routing does not get deactivated when a default route related to a specific controller/method pair is added. That being said, if a URL does not fit the route $route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2', then the same URL is run through CI's default routing mechanism as a fallback, so it still takes me to the resend method of the sms controller. To prevent this from happening, I needed to add another custom route that follows all others related to the sms resending, that redirects any other url to a different controller+method. If this controller doesn't exist, you get the default 404 page.
So the final /config/routes.php file:
$route['sms/resend/(:num)/(:any)'] = 'sms/resend/$1/$2';
$route['sms/checkoperator/(:num)'] = 'sms/checkoperator/$1';
$route['sms/(:any)'] = 'somewhereovertherainbow';
I think the rout file is just for rerouting. Your URL don't fits the routing Conditions so it don't gets rerouted! So it goes the normal way wich is the same (in this case!)
Something like this could work!
(! :num) /(:any) '] = error page (or not existing page)
So every request wich doesn't start with a number gets redirected to the error page!
The syntax might be wrong!
This would work great :
$route['sms/resend/[^0-9]/(:any)'] = 'errorpage';
You have to replace the error page by something ;)

Logging behavior of Codeigniter | show_404()

I use a controller to do some custom uri routing for pages, it's currently working great.
Here is a stripped down version of the controller which uses PHPTAL as it's template engine.
public function index()
{
$this->tal->display('index');
}
public function view($url)
{
$this->loadView($url);
}
private function loadView($url)
{
if (file_exists(ROOTPATH . 'webroot/' . $url . '/index.html'))
{
$this->tal->display($url . '/index');
}
else
{
show_404();
}
}
The problem
I recently noticed that the following error was appearing in my logs each time the page controller was accessed:
ERROR - 2013-02-06 10:58:23 --> 404 Page Not Found -->
I found this strange as the page displays as expected and there is definitely no 404 header and the network panel shoes no 404 status.
I finally narrowed this down to the show_404() helper method being called in the loadView() method. Removing that line stops the errors from appearing in the log file altogether.
This §show_404()§ should only be executed if the view file cannot be found, in which case it should show the 404 error page, which it does. However the logging part of the method appears to be being executed on every call to the page controller, regardless of whether the loadView() method is called or not.
Example
I access the index() view in the browser, everything appears to work fine, the correct view file is loaded, there are no errors. However a 404 error message is logged from the loadView() method. The loadView() method isn't even being called from the index() method, yet the existence of its output in the log files seems to indicate that it is being executed.
The default behaviour of show_404 is to insert a log entry when its called. There's a second optional parameter to disable it, so if you just want to show the 404 page without logging it call it like this:
show_404('', false);
The first parameter is the string that would be appended after the --> in the logs, so you can leave that empty too.
As for why the _loadView function might be get called:
If you are using urls without the index.php in them (empty $config['index_page']) then every static asset (image, js, css, etc.) that the webserver server can't find (depends on concrete rewrite rules) will be passed to the php script and probably generate a 404 error there.

Only allow URL's specified in routes to be seen in Codeigniter

If I have a controller called articles, which has a method called view_articles, a user can type in http://example.com/articles/view_articles/some-post and have it return a page.
I have specified a route to be http://example.com/article/post-name. How can I make it so that only the URL specified in the route is visible? Is there a way for articles/view_articles/some-post to show a 404 instead of showing the same page as the route URL?
I am trying to prevent duplication for SEO purposes.
You can always make default routing to a 404 page by correctly defining routes in your routes.php file:
$routes['article/(:any)'] = 'articles/view_articles/$1';
$routes['(:any)'] = 'main/e404';
As stated by CodeIgniter user guide:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
So you can basically define all you want to be seen at the beginning of the file and block everything else on the last line.
As for your main(can be any other) controller's 404 method -
function e404() {
show_404();
}
Use $this->uri->segment(n) as part of the URI class inside of view_articles to redirect traffic to your route if the URI contains view_articles as it's second segment.
I'v done that in other tricky way, first of all you should add some code to __construct function in sys/core/Controller.php
you can check whether the requested url is in routes or not by this code
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}

Categories