codeigniter form post from a folder inside controller not working - php

So, here is my problem.
In codeigniter, I have a folder inside controllers named admin and the controller admin.php inside that.
The problem is when i try posting a form from this controller to a controller outside admin folder, it does'nt works. I m really getting frustated trying to figure out where exactly i am doing wrong.
my routes.php is-
$route['admin/(:any)'] = "admin/$1";
$route['(:any)'] = "welcome/$1";
$route['default_controller'] = "welcome";
$route['404_override'] = '';
and my base url is $config['base_url'] = 'http://localhost/ci_extend/';
Actually the form is getting posted to self.
I know, surely there is routing problem but just cant figure it out. Can someone help me out?

Since you have your controller in your admin folder named admin.php
you should route it as below
$route['admin/(:any)'] = "admin/admin/index/$1";
I've included function name i.e. index , I guess the function name is required, give a hit without function name if you want to, but I don't see any problem in keeping it.
Accepted Answer
Change $route['(:any)'] = "welcome/$1"; > $route['welcome/(:any)'] = "welcome/$1"; & if using <form> make sure CSRF either disabled or you included CSRF token in the form

Related

Codeigniter 3.1.0: Only default route is working

I will preface my question by saying I have searched high and low for a solution to my problem, including, but not limited to, Stackoverflow, YouTube and Google.
My issue: Only my default route is working in Codeigniter. My default route is set to a controller called Home which loads a home.php view. That works fine. I have another controller called Pages which as of now has only one method, Contact which points to view of the same name.
My routes.php file looks like this.
$route['default_controller'] = 'home/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// All custom routs go below this line.
$route['contact'] = 'pages/contact';
If I should change the default rout to 'pages/contact' my contact page shows.
My setup is as follows:
Windows 10
XAMMP 5.6.12
PHP 5.6.12
Any help would be greatly appreciated.
Default controller name should be like this $route['default_controller'] = 'home'; not like your $route['default_controller'] = 'home/home';
And then try it the url www.site.url/pages/contact is working or not if working then try
$route['contact'] = 'pages/contact';
Also you can debug your route with this link.

URL routing in PHP Codeigniter

I am having entry in my route.php like - $route['admin/students'] = 'view_student'. Here view_student is controller name. Now when from "localhost/school/admin" page I call Students, than everything works fine; But when I change my route like - $route['/school/admin/students'] = 'view_student', and call it from "localhost/school/admin" page as Students, than 404 page is shown. Whats wrong in here?
Try this code it might help you :
Here dashboard is the name of controller
//this will route as localhost/appFolder/admin/index
$route['admin'] = 'dashboard'; // for your index page
//this will route as localhost/appFolder/admin/method_name
$route['admin/(:any)'] = 'dashboard/$1';
//this will route as localhost/appFolder/admin/method_name/param1
$route['admin/(:any)/(:any)'] = 'dashboard/$1/$2';
Link the route Like
// for your index page
// for your other pages
To link the other controller defined just like
school is your ci root, so if you define $route['/school/admin/students'], it will seek school class with admin function, that never exist, instead of admin route.
you should read the documentations first before make any step,
https://www.codeigniter.com/userguide3/general/routing.html

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

Remove or hide controller name from url in codeigniter

How can I remove controller name from url . I have two controller
home and admin
and the url's are
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
home/post
and
http://domain.com/likes/admin/ad_managment/edit/2
http://domain.com/likes/admin/meta_tags_home/edit/2
admin/ad_managment admin/meta_tags_home
I have already used this
$route['(:any)/(:any)/(:num)'] = "home/post/$1";
It works for this URL
http://domain.com/likes/home/post/sports/20-Athletes-Who-Profited-While-in-College-/12
admin is not working. Basically I want to remove home/post Leave admin controller it doesn't matter
try this
$route['(:any)/(:any)/(:any)'] = "home/post/$1";
Try this example
$config['base_url'] = 'http://your-site.com/';
And in your routes.php
$route['default_controller'] = 'main';
Through this method you can remove controller name from url.
check user guide of codeigniter and in this example see how the routing done.
Read more about code-igniter routing

Routing with modules with codeigniter

I'm working with HMVC with wiredesignz plugin and have the following url mysite.com/login and it views my login form. I have the form to post to the submit method in the login controller inside of the user module. I'm trying to figure out what the route would be for this to work because inside of the submit method I have it echoing a string for testing purposes. The first route works beautifully but the second route presents a 404 Page not found. Any thoughts on how to correct this.
$route['login'] = 'user/login';
$route['login/submit'] = 'user/login/submit';
Try this one
$route['login/(:any)'] = 'user/login/$1';
$route['login'] = 'user/login';
or try to change the action of form to submit
$route['submit'] = 'user/login/submit';
$route['login'] = 'user/login';
I was able to fix my issue by noticing that my submit function was named as a private function and not listed as public.

Categories