Two or more loaded controllers with the same name login models are not working.
E.g I have two models from same name one is login_model and other is in admin -> login_model.php then I want to load both in a single controller.
How can I do that?
try this
$this->load->model('admin/login_model' , 'myinterest');
The second parameter (optional) is used to call the method.
ex:
$this->myinterest->get_users();
You can do this like this:
$this->load->model("login_model","login_model");
$this->load->model("admin/login_model","admin_login_model");
You need to place admin model in another folder, say in admin folder
so your code will be like below
$this->load->model('login_model');
$this->load->model('admin/login_model');
This will call both model in same controller
You can use this as well:
$this->load->model("admin/login_model","login_model");
Related
I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.
For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.
$data['id'] = $this->session->userdata('user_id');
pass this in create() function of department controller.
i'm loading user model in __construct() function also.
Thanks in advance.
You can do it using two ways.
1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.
2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.
Ist Controller - Test.php
here i defined a variable as a public and this variable contains userid value.
2nd Controller - Test2.php
here we will require Test.php file
require('Test.php file path');
and then create object and then we can access Test.php file variables and methods inside Test2.php file.
But i will suggest you first way it`s very easy way and multiple advantages.
Hope it will work for you.
I developed a project in php codeigniter. The project is almost complete but now my manager wants me to completely remove or hide the controller names in the URL. My current URL looks like this:
http://www.sitename.com/Controller_Name/function_name
and my manager wants it to look like this:
http://www.sitename.com/function_name
Note that I have more than 10 controllers in my project and many many functions. I want a method that applies to all. HELP PLEASE.
You can do this using $route.
If you have only one Controller and in that controller you all the functions you can write something like this:
$route['(:any)'] = "Controller_Name/$1";
If you have many Controllers you need to specify for each function to what controller to point. Like this:
$route['function1'] = "Controller_Name1/function1";
$route['function2'] = "Controller_Name1/function2";
$route['function3'] = "Controller_Name3/function3";
And you can't have duplicates in $route
This $route array should be located here: application/config/routes.php.
You can check more about routes on the CI documentation here: https://www.codeigniter.com/userguide3/general/routing.html
You have to specify your controller and method name in routes file like below
$route['function_name'] = 'controller_name/function_name';
You have to write this for each function in controller.By doing you get two advantages
1) Controller name will be hidden in your url
2) you can call some method by its name only.No need to use controller name every time.
I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.
The following URL takes me to the default controller as specified in my config/routes.php.
blog/index.php
According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:
blog/index.php/blog_login
Here is my controller class, named blog_login.php:
class Blog_login extends CI_Controller {
public function index()
{
echo 'It works!';
}
}
But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?
http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.
According to the documentation, simply appending the name of another
controller saved in controllers/ is all that is needed
This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:
domain.com/index.php/blog_login
This will never work:
blog/index.php/blog_login
Index.php (unless you remove it via .htaccess) always follows right after your domain.com
Finally, you don't need to specify routes unless you're doing something non standard. So
domain.com/index.php/blog_login - calls the index() function in your Blog_login controller
domain.com/index.php/blog - calls the index() function in your blog controller
domain.com/index.php/blog/search - calls the search() function in your blog controller.
None of the above examples need an entry in routes.php
When u call:
blog/index.php/blog_login
you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:
controller_name/controller_method
So, if you wanna call your blog_login controller just call it like this:
blog_login/
Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.
I want to get the current controller name that handles the current action.
but the in my case I will look for the current controller in my main.php in my layout files.
this is my small view of my directory structure to give you an idea where is my layout files and the file where i will put my codes in searching of my controller name
/protected
/themes
/mylayout
/layouts
main.php
column1.php
column2.php
/site
index.php
Is this possible? im trying the following codes but i failed to get my current controller name...
echo Yii::app()->controller->getId;
echo Yii:app()->getController->id;
echo Yii:app()->controller->uniqueID;
thanks
Like this
Yii::app()->controller->id
or
Yii::app()->getController()->getId()
http://www.yiiframework.com/doc/api/1.1/CApplication#getController-detail
Controller Id :
$this->id
Here $this refers to controller.
And
For getting action id :
$this->action->id
<?php echo $this->getUniqueId();?>
this will show current controller
You're not actually required to use the static function. Whenever in a view( or template) you can use echo $this->getUniqueId(); to get the unique controller ID.
Yii2:
Yii::$app->controller->id
(Documentation: Application and Controller)
Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc