Welcome Guest, Not a member yet? Register   Sign In
404 when accessing a function in a controller
#1

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-aut...-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.
Reply
#2

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.
Reply
#3

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 
Reply
#4

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

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
Reply
#6

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'); 
Reply
#7

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/helpe...tml#url_to
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#8

(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
Reply
#9

(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/helpe...tml#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
Reply
#10

(This post was last modified: 09-14-2022, 12:55 PM by davecoventry.)

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.
~ Dave
Reply




Theme © iAndrew 2016 - Forum software by © MyBB