I have two controllers. Let's say A and B.
For clarity's sake I want to run some functions in B controller but I have trouble getting back to A.
Of course I could just run
$this->load->view('pages/examplepage');
But this example page has to get data from database and of course it gives me an error that it doesn't have the necessary data.
Now I could just run
$data['exampledata'] = $this->example_model->get_data();
$this->load->view('pages/examplepage', $data);
Now for couple of rows it's okay but isn't there a better way? What am I missing? Can't I just run the A controller's function from B controller and let the already existing function do the job?
Hope this will help you :
user codeigniter url helper redirect()
Example :
Controller A
class Abc extends CI_Controller {
public function first()
{
redirect('xyz/some_method');
}
}
Controller B
class Xyz extends CI_Controller {
public function some_method()
{
redirect('abc/first');
}
}
for More : https://www.codeigniter.com/user_guide/helpers/url_helper.html
Related
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();
}
}
I'm trying to create a new site and it includes database, as it said every time you want to access your database you need to do it in your Model but I don't know why I am having a problem with my Model. Please the codes below.
Controller:main
class Main extends CI_Controller {
public function index()
{
$this->home();
}
public function home(){
$this->announce($data);
}
public function announce(){
$this->load->library('table');
$this->load->model('home_model');
//echo "test";
}
Model:home_model.php
class Home_model extends CI_Model{
echo '';
}
after running the program. localhost/ci_gcc/main/announce or localhost/ci_gcc/main I got this error.
Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\ci_gcc\application\models\home_model.php on line 4
but if I didn't type anything inside the model class there is no error.
Thus anyone have a solution for this?
You are doing a bad attempt. Don't echo like this in model class.
$this->load->model('home_model');
This will load your model class. Inorder to do any operations inside the model class, you have to write functions inside it. And you can call that function from your controller like,.
$this->home_model->functionname();
I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views
this is my problem. I'm using codeigniter and this is my structure. I have 2 classes in 2 files
class Model_setup extends CI_Model
{
//functions
// an update function
public function update()
{
// update stuff
}
}
// OTHER FILE
class Tests extends Model_setup
{
// other functions....
// a totally different update function
public function update()
{
// update a specific set of stuff
}
}
I want to know if and how it is possible to reference these two separate update functions. In a separate controller from these two, say the Places_Controller how would you tell the difference between these two class methods and how would you make sure that you are only using one or the other of the two updates? Thank you for the help in advance.
Assuming you're loading both models, you just reference them by name:
$this->Model_setup->update();
will refer to that first method, while
$this->Tests->update();
will refer to the second one.
So I was enlightened by a friend about how to solve this. This doesn't need any codeigniter frmaework stuff to be made to work correctly. The following would work correctly:
class Model_setup extends CI_Model
{
//functions
// an update function
public function update()
{
// update stuff
}
}
// OTHER FILE
class Tests extends Model_setup
{
// other functions....
// a reference function to the parent function
public function parent_update($x)
{
// update a specific set of stuff
parent::update($x);
}
// a totally different update function
public function update()
{
// update stuff
}
}
Now from the outside world, say another controller in the framework you can call the following once everything has been loaded. $this->tests_model->parent_update($x) when you wish to call the parent version and $this->tests_model->update when you wish to call the update function for the Tests model. This worked and I have tested this.
I'm having a litle problems with my concepts of OOP. I'll try to explain the best I can.
I have this class
class Application_controller extends CI_Controller{
public function addItem(){
"some code to add the item to the database (working)";
}
}
And I have another class, both controllers:
require_once 'application_controller.php';
class Contact extends Application_controller{
public function __construct(){
parent::__construct("variables needed");
}
}
And in the View add of the contact I added the following action contact/addItem.
Ok, now here's what I know about OOP in general.
Isn't the method addItem supposed to be part of the Contact class because its extends Application_controller?
I'm asking because when I submit the form I get no action, and when I add the method addItem in the class Contact overriding the parent one it works.
The reason you get no action is that codeigniter doesn't find a method addItem in your Contact class (update: this is probably due to the way CodeIgniter routing works). The solution would be to make addItem a generic method in a Model that stores data in a table, move it to a Model, and load the model in your controller.
Create application/models/writeModel.php
class writeModel extends CI_Model{
function addItem(){
// code here
}
}
In your controller:
class Contact extends Controller{
function __controller(){
parent::Controller();
$this->load->model('writeModel');
}
function somefunction(){
$this->writeModel->addItem(); // call the method here
}
}
Reference: CodeIgniter Models
The problem here (other then the several syntax errors in the OP) is likely to be that "Contact" can not extend "Application_controller" because it does not know it exists. If we setup a test like this:
/controllers/Test.php
class Test extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
echo 'test';
}
}
/controllers/TestTwo.php
require_once("Test.php");
class TestTwo extends Test
{
function __construct()
{
parent::__construct();
}
function index()
{
parent::index();
echo ' and test two';
}
}
We will get the desired output of "test and test two" by navigating to appurl/TestTwo/. This is because TestTwo knows of Test. Removing the require(); line from TestTwo.php will break the relation.
Removing the index() function from TestTwo will then result in only "test" being output by navigating to appurl/TestTwo/.
I found an answer to some similar question on the Codeigniter forums. It says this
your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.
Sohaib,
The link for the post is http://codeigniter.com/forums/viewthread/102718/#518120
I don't know how but this is working today. It was probably the server. Just needed a restart.
Its Solved, just by start the server today and start developing LOL. Thanks for youre time guys.
Regards,
Elkas