How to call sub controller from main controller - php

I have main controller.
Controller Folder: main.php
the usual coding is you will call everything from main controller.
class Main extends CI_Controller{
public function index(){
//line of codes
}
public function login(){
//line of codes
}
public function register(){
//line of codes
}
}
So instead of doing this, I'm thinking to make login and register as sub controller and call them from the main controller but I don't know how to call them.
Controller folder: main.php, login.php, register.php
Please paste some line of code and if you can explain it as well.

Related

How I create a login controller? Codeigniter

I'm having some troubles with codeigniter and the MVC model. In my webpage I have a main controller with functions that handles the usual navigation menu with different views home page,about,support... but I have a login view.
I'm not sure if this separation is correct or I should create a controller for each view.
How do I call the login controller, I'm using base_url('main/myView/'); to call the functions inside the main controller, but if I call the login controller from the login view base_url('login/foo'); it does not work.
I'm new to codeigniter and I read their tutorial but I still not sure when should I create a new controller.
Thanks
There are many auth libraries for Codeigniter. I'm sure use the library is better than you write it.
Codeigniter 3.x Authentication Library?
https://github.com/benedmunds/CodeIgniter-Ion-Auth
https://github.com/jenssegers/codeigniter-authentication-library
You don't need to create a multi-controller for each view. you can do it all with one controller.
If you need another controller you can create a new controller and assign it in routes.php. For now, I'm creating 2 controller. Remember controller name start with the capital letter
// pages controller
class Pages extends CI_Controller { // `application/controller/Pages.php`
public function __construct(){
parent::__construct();
}
public function index(){
// default_controller
}
public function about(){
// pages/about
}
public function support(){
// pages/support
}
}
// admin controller
class Admin extends CI_Controller { // `application/controller/Admin.php`
public function __construct(){
parent::__construct();
}
public function login(){
// admin/login
}
public function logout(){
// admin/logout
}
}
Assign url to the controller in application/config/routes.php
$route['default_controller'] = 'pages'; // call lowercase letter
$route['about'] = 'pages/about';
$route['support'] = 'pages/support';
$route['login'] = 'admin/login'; // call lowercase letter
$route['logout'] = 'admin/logout';
If you call base_url('login'), admin controller login function will work
If you call base_url('about'), pages controller about function will work

Called controller can't access anything

in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}

Any action is redirected to the root

I am using CakePHP for the first time and I have a trouble with requests.
I have for example this simple controller:
class TestController extends AppController
{
public function index() {
}
public function edit() {
}
public function view() {
}
}
Index is rendering right but when I call /test/edit or /test/view I am redirected (302) to the root of my webpage. Views for each action are created.
It's the same for any other controller in my app.
I cannot figure out why. Do you have any ideas, suggestion what should I check?
Thi was the problem $this->Auth->allow(['edit']); now it is working.

Accessing controller's function

I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}

couldn't load model properly codeigniter

I have loaded the 2 models on a function in a controller. In my first controller, loading both models works. Now, when I load the same models in the same order, my second controller makes an error which is thrown to ajax. When I delete the user_model - the first model, it works fine: ajax fires its success function.
How come it throws error when I try loading the user_model? It works perfectly fine on a different controller.
Here's the code of the constructor method in my second controller
public function __construct(){
parent::__construct();
$this->load->model('friend_model');
$this->load->model('user_model');
if (!isset($_SESSION)) session_start();
}
It's the same constructor method that I used in my another controller.
the error I got from the $.ajax error function is parsererror.
You will load all models,libraries and helpers file in constructor in controller file
<?php
class usercontroller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
}
?>
it's works fine.

Categories