thanks to help me.
In CodeIgniter, I have a Controller named "Idee" in which there is this function :
public function index(){
$this->load->model("idee");
echo "Doesn t go ahead";
$data['idees'] = $this->idee->getAll();
var_dump($data);
$this->load->view("view_home", $data);
}
But when I try the URL, the page is blank, and even the echo is not reached.
It is weir because I already did this on others page, and it's working.
Could you help me please ? Thanls a lot !
You need to name your model something different from your controller. It is failing because you are trying to redeclare the same class (Idee). Usually in CodeIgniter you would call your model something like Idee_m (the file and class).
Related
i'm from codeIgniter , but for an internship I have to debug an ongoing website using cakePHP.
I'm familiar with mvc but currently stuck and unable to find any solution even after searching.
What I want is to be able to call a function in a controller (easy task with codeIgniter) ,but during the debug I noticed that the controller was called, but not the function:
<?php
App::uses('AppController', 'Controller');
class FournisseursController extends AppController { //debug stop here
public $helpers = array('Html', 'Form');
public function index() {
$this->set('Personnes', $this->Fournisseur->find('all'));
}
public function addFournisseur() {
//contain some code , but not usefull for this problem
}
}
}
?>
after the 2 first line , the associated view is displayed (it's a weird concept to always have a view displayed, but I guess cake php work this way).
and debug dont even start on the targeted function.
I call it from a view:
<form action="../../Fournisseurs/addFournisseur" method="post">
<input type="submit">
</form>
I know it's not really good to call a controller from view , but in that case I need the user to enter some data to send it in a database, I dont know any other way.
I already read the doc concerning controller and it didnt help.
One last thing , if I change the name of the function in the controller , the controller will not be called : an error message explain the the method dont exist (so for me the controller know I want to call the function, because the controller need it to run).
it's been a day since i'm stuck on this and i'm pretty sure it's a stupid mistake.
thanks for reading this even if you dont answer , and sorry if I made any english mistake, it's my first post and i'm not a native speaker.
I was only relying on Xdebug ,despite the breakpoint it was not stopping in the function ,so I thought it was not called (the function and the code inside it), but with the help of the debug($data) function the breakpoint worked and I was able to keep debuging in the function.
I dont know why Xdebug acted this way ,but I will not rely only on it now.
thanks to everyone who helped, I can keep going now!
You can also use Form helper like below, where you can define the contoller and action name:
<?php echo $this->Form->create('Fournisseur', array('url' => array('controller' => 'Fournisseurs', 'action' => 'addFournisseur'))); ?>
// Form elements
<?php echo $this->Form->end(); ?>
I'm posting this after my hair has been ripped out, ran out of rum, and tried everything I can find on google. I've been developing a site using codeigniter which makes use of templates. I've built the backend first and all is working properly there. So now i've started on getting the front end working which is where I'm hitting the issue.
I've created a controller called pages.php which is going to parse the uri string of the current page, use my library to get the page data from the database, then display it. My pages are all created through an editor on the back end and stored in the database.
So here's the pages controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("pages");
}
public function display_page()
{
$page_slug = $this->uri->segment(1);
$data["joes"] = "Here's joes first variable";
$this->pages->get_page($page_slug);
}
}
and here's the error message i get when i hit my url like this demo.mydomain.com/joes-test
and here is how my routes are set up. $route['(:any)'] = 'pages/display_page';
My Pages.php library works perfect on the back end but it's a large file. I've only posted the get_page function below. If you need to see everything let me know. But i dont believe the issue has anything to do with the library itself.
public function get_page($slug){
$objPages = new pages();
$objPages->get_object('slug="'.$slug.'"');
return $objPages;
}
[EDIT] If i place the following inside my homepage controller it works. But the calling function needs to be inside the library.
$this->load->library('pages');
$the_page = $this->pages->get_page("joes-test");
I want to call $this->get_object("joes-test") but this doesn't work. get_object() is an inherited function inside the library.
Now oddly enough. The code i put above will NOT work if i do the exact same thing inside the pages controller
any help leading to a solution would be awesome. I'm under a time crunch and pay to get some assistance. Thanks in advance.
No you can't use the same name with controller and library. please choose another name. for example Mypages for you controller name.
change your routes
$route['(:any)'] = 'mypages/display_page';
then call your controller.
http://demo.mydomain.com/joes-test
I think there nothing wrong with library uri, because as codeigniter official website say: This class is initialized automatically by the system so there is no need to do it manually.
I don't know about lib pages, but how about use
$this->load->view(<file-html>);
and if you want to passing data in variable, you can add variable like this
$this->load->view(<file-html>, $data);
Hope this help, Cheers
I'm trying to display the latest Topic that is related to a Theme. I did this before and I tried the following code in the ThemesController (Index method, Sinse it's the homepage),
$topics = Theme::with('topic', 'lastTopic.user')->get();
$themes = Theme::all();
return view('themes.index')->with('themes', $themes)->with('topics', $topics);
And the method I'm calling is this.
public function lastTopic()
{
return $this->hasOne(Topic::class)->latest();
}
The method sits inside the Theme Model, It doesn't matter if i put the method in the Topic Model of the Theme Model, It still gives me the same error. So what is causing this issue? This is for my homepage so the route is this Route::get('/', 'ThemesController#index')->name('home');. I know it is something I've done before but I can't figure it out. Thanks in advance
You should add a topic method in your Theme model (if you haven't already did).
Something like:
public function topic()
{
return $this->hasMany(Topic::class);
}
while working yesterday with codeIgniter I found some strange(I don't know what to call),maybe I don't know whether it is normal or not as am a rookie using this framework.Below is my controller class.
class Posts extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model( 'post' );
}
public function index() {
// echo "<pre>";
// print_r($data['posts']);
// echo "</pre>";
$data['posts']=$this->post->get_posts();
$this->load->view( 'post_index', $data );
}
public function post( $postID ) {
$data['post']=$this->post->get_post_by_ID( $postID );
$this->load->view( 'post', $data, FALSE );
}
I found that strange in function "post" the reason is simple if I change the function name then I will get the error-page not found.
Why is that?? Is it necessary to have function name and view name to be same.As I told am a beginner to this framework.So please co-operate and provide your precious feedback.
Function name and view name do not need to be the same. Your view can be anything you want it to be so long as you call the filename (and sometimes path) correctly :)
The biggest problem I see here is you are trying to access some object called posts, which I do not see defined. I have a good hunch you're looking for the "input" object provided by code igniter.
That being said, replace:
$this->post->get_posts();
With:
$this->input->post(NULL,true);//return all post objects with cross site scripting scrub
And replace:
$this->post->get_post_by_ID($postID);
With:
$this->post->post('postID',true);//same as above, except again, with XSS scrub enabled.
If this is not your answer and you need me to re-evaluate, please let me know in a comment and I'll redo the answer.
With regards to your page not found issue, it's because Codeigniter automatically assumes the paths depending on what you name your functions. It uses the following convention by default (routes.php allows you to override this in config/routes.php)
site.com/index.php/controller_name/method_name/param_1/param_2/param_n
Or if you have mod_rewrite taking out the front controller
site.com/controller_name/method_name/param_1/param_2/param_n
Since the view is loaded from the controller function itself, if you change the name of that function, then the URL will no longer be able to "find" it, hence the error.
I want to cache a query in CodeIgniter. What I did for my test is make a controller, that I named show.php:
class Show extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('rejaal_show');
}
public function _remap($method = '',$param = array())
{
$method = intval($method);
$this->output->cache(5);
var_dump ($this->rejaal_show->temp($method));
}
}
And a model that I named rejaal_show.php:
public function temp($id)
{
$this->db->cache_on();
$this->db->where('id',$id);
$query = $this->db->get('system_store_table');
return $query->result();
}
When I call http://localhost/rejaal/show/1 for the first time, it will show a result, but when I call it for the second time, it does not show anything.
I should delete the query cache file to show it again? How should I solve this problem?
With special thanks for your attention.
Can you confirm that you have set $db['default']['cachedir'] to the path of a writable folder in application/config/database.php and that when the query is first run it creates a cache file in there?
The only other reason I can think of for it failing is by your use of the _remap override. I have not used db caching using _remap, but know that CodeIgniter creates a folder called controller+action in your cache folder, and might not be handled very well if using remap? Someone correct me if I am wrong about this.
In the CodeIgniter User Guide page for Web Page Caching, it says:
Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
Do your var_dump inside a view.