CodeIgniter Forums
404 when accessing a function in a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: 404 when accessing a function in a controller (/showthread.php?tid=83014)



404 when accessing a function in a controller - davecoventry - 09-11-2022

Hi,
This is my first iteration of Codeigniter and I'm having trouble calling a function inside a controller.
I've used this tutorial more or less verbatim to set up: https://www.positronx.io/codeigniter-authentication-login-and-registration-tutorial/
I have modified the 'welcome view' (https://example.com) with 2 buttons, 'Access' and 'Register'. 
I have 2 Controllers, 
Code:
SignupController.php & SigninController.php
In the signup controller, there are 2 public functions:
PHP Code:
public function index() 
which calls the 'form' helper and invokes the 'signup' view, and
PHP Code:
public function store() 
which verifies the input data and inserts it to the database.
The signup view contains the form html:
Code:
<form action="<?php echo base_url(); ?>/SignupController/store" method="post">
When I click the 'Register' button on the welcome page, this correctly calls the signup page: example.com/signup
But when I click the 'Submit' button of the form, example.com/SignupController/store, I get 404 File not found.
Can anyone suggest where I'm going wrong?
The Routes.php file has
PHP Code:
$routes->get('/signup''SignupController::index'); 
and I have tried to add
PHP Code:
$routes->get('/signup''SignupController::store'); 
, but it makes no difference.


RE: 404 when accessing a function in a controller - iRedds - 09-11-2022

1. You have registered a route for the HTTP GET method. But the form is submitted using the HTTP POST method.
2. You are registering the /signup route, but using the /SignupController/store address.


RE: 404 when accessing a function in a controller - davecoventry - 09-11-2022

Thanks very much for the prompt reply.
Are you referring to
PHP Code:
[code]$routes->get('/signup', 'SignupController::store'); [/code]?

The 404 error was appearingeven without adding that to the 'Routes.php' file.

Dave 



RE: 404 when accessing a function in a controller - JustJohnQ - 09-11-2022

Add a (general) post route in routes.php:
Code:
$routes->get('/SignupController', 'SignupController::index');
$routes->post('/SigupController/(:any)', 'SignupController::$1');



RE: 404 when accessing a function in a controller - davecoventry - 09-11-2022

In View/signup.php
PHP Code:
<form action="<?php echo base_url(); ?>/SignupController/store" method="post"

In app/Config/Routes.php:
PHP Code:
$routes->post('/SigupController/(:any)''SignupController::$1'); 

Result (called using form->Submit): 404, File not found.
Result (using https://example.com/SignupController/store in Browser): 404, File not found.


In View/signup.php
PHP Code:
<form action="<?php echo base_url(); ?>store" method="post"


In app/Config/Routes.php:
PHP Code:
$routes->get('/store''SignupController::store'); 

Result (called using form->Submit): 404, File not found.
Result (using https://example.com/store in Browser): Function called correctly, page displayed.

In View/signup.php
PHP Code:
<form action="<?php echo base_url(); ?>/store" method="post"


In app/Config/Routes.php:
PHP Code:
$routes->post('/store''SignupController::store'); 

Result (called using form->Submit): 404, File not found.
Result (using https://example.com/store in Browser): Function called correctly, page displayed.

~Dave


RE: 404 when accessing a function in a controller - JustJohnQ - 09-12-2022

Probably because of a typo you got the error, I missed the 'n' in /SignupController and I guess you copy/paste it.
Code:
$routes->post('/SigupController/(:any)', 'SignupController::$1'); 



RE: 404 when accessing a function in a controller - ozornick - 09-12-2022

Advice. Never use hard URLs in links. Use the url_to("login.signup") function when adding route names ['as' => 'login.signup']
You will have fewer problems and more flexibility in generating
See https://codeigniter.com/user_guide/helpers/url_helper.html#url_to


RE: 404 when accessing a function in a controller - davecoventry - 09-12-2022

(09-12-2022, 06:05 AM)JustJohnQ Wrote: Probably because of a typo you got the error, I missed the 'n' in /SignupController and I guess you copy/paste it.
Code:
$routes->post('/SigupController/(:any)', 'SignupController::$1'); 

Thanks, John.

Yes, I just copied-and-pasted and didn't notice the typo.

Still getting the 404, though.

~Dave


RE: 404 when accessing a function in a controller - davecoventry - 09-12-2022

(09-12-2022, 11:43 AM)ozornick Wrote: Advice. Never use hard URLs in links. Use the url_to("login.signup") function when adding route names ['as' => 'login.signup']
You will have fewer problems and more flexibility in generating
See https://codeigniter.com/user_guide/helpers/url_helper.html#url_to

Thanks: I'll bear that in mind.

As I mentioned, this is my first implementation of Codeigniter, so any tips are very welcome.

~ Dave


RE: 404 when accessing a function in a controller - davecoventry - 09-14-2022

Thanks.

I've got it working.

Using
PHP Code:
$routes->post('/store''SignupController::store'); 
in Routes.php did the trick.

(I must have had a typo in there when I tried earlier).

Sorry for the confusion.