404 when accessing a function in a controller |
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 PHP Code: public function index() PHP Code: public function store() The signup view contains the form html: Code: <form action="<?php echo base_url(); ?>/SignupController/store" method="post"> 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'); PHP Code: $routes->get('/signup', 'SignupController::store');
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.
Thanks very much for the prompt reply.
Are you referring to PHP Code: [code]$routes->get('/signup', 'SignupController::store'); [/code]?
Add a (general) post route in routes.php:
Code: $routes->get('/SignupController', 'SignupController::index');
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
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');
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
(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. Thanks, John. Yes, I just copied-and-pasted and didn't notice the typo. Still getting the 404, though. ~Dave
(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'] Thanks: I'll bear that in mind. As I mentioned, this is my first implementation of Codeigniter, so any tips are very welcome. ~ Dave
Thanks.
I've got it working. Using PHP Code: $routes->post('/store', 'SignupController::store'); (I must have had a typo in there when I tried earlier). Sorry for the confusion.
~ Dave
|
Welcome Guest, Not a member yet? Register Sign In |