Codeigniter Front End Controller Not working with libraries - php

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

Related

cake php calling a function, but the controller wont execute it

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(); ?>

Why is Yii2 framework showing a 404 error when creating custom view page?

I'm attempting to create a custom display in yii2 framework using this code in my site controller:
/******/
public function actionChartDisplay()
{
return $this->render('chartDisplay');
}
for testing purposes I pasted the form name in my actionAbout function as a parameter to the render function in it. It worked with this:
public function actionAbout()
{
return $this->render('chartDisplay');
}
But I need to create many custom views in yii2 and this won't be a solution.
This is the error I get
I'm curious as to why it is. Since I was following this tutorial and came across this weird behaviour.
My 'chartDisplay.php' file is merely a "hello world" that does work with the action about function.
in yii2, the controllers and actions with multiple words, that are marked by capital letters are divided by - in your request, so in your case the route would be some/chart-display
Apparently as #SmartCoder pointed out it was an error on how Yii2 Handles the action functions in its controller however I didn't mark his answer as the solution right away because implementing it resulted in an error. So aside from that I'm posting the way I solved it.
So instead of using chart-display I simply changed it for "charts" like this:
public function actionCharts(){
return $this->render('charts');
}
Changed the name of my file so it fits to charts.php and it worked.

Codeigniter how can I make this route work

I've looked around in the documentation and a few other places and I am not sure how to achieve the results I want. I have a domain name set up that will only server public profile pages to visitor. When someone comes straight to the site I want it to display a welcome screen telling more about what the website is about. (example.com) When someone visits example.com/my-public-page I want it to run a default controller that grabs the 'my-public-page from the url, searches the database for a user with that info in a column and displays their info.
I would image that I just set the default_controller to my controller and then check for a second string in the url. If its present search for that in the database. After looking around I am not sure how to make this work and not sure what to search for to get the results I need.
Thanks for your help,
After defining an encryption key for sessions in config.php you can use something like this:
class Wellcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this -> load -> library('session');
if(!$this->session->userdata('visit_time')) {
$this->session->set_userdata('visit_time', time());
redirect('first_visit');
}
}
}

Relation between function name and view name(controller class)/Working of load->view and load->model in codeIgniter framework(php)

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.

codeigniter routing

I am currently working on CMS for a client, and I am going to be using Codeigniter to build on top of, it is only a quick project so I am not looking for a robust solution.
To create pages, I am getting to save the page details and the pull the correct page, based on the slug matching the slug in the mysql table.
My question is however, for this to work, I have to pass this slug from the URL the controller then to the model, this means that I also have too have the controller in the URL which I do not want is it possible to remove the controller from the URL with routes?
so
/page/our-story
becomes
/our-story
Is this possible
I would recommend to do it this way.
Let's say that you have : controller "page" / Method "show"
$route['page/show/:any'] = "$1";
or method is index which I don't recommend, and if you have something like news, add the following.
$route['news/show/:any'] = "news/$1";
That's it.
Yes, certainly. I just recently built a Codeigniter driven CMS myself. The whole purpose of routes is to change how your urls look and function. It helps you break away from the controller/function/argument/argument paradigm and lets you choose how you want your url's to look like.
Create a pages controller in your controllers directory
Place a _remap function inside of it to catch all requests to the controller
If you are using the latest version of CI 2.0 from Bitbucket, then in your routes.php file you can put this at the bottom of the file: $routes['404_override'] = "pages"; and then all calls to controllers that don't exist will be sent to your controller and you then can check for the presence of URL chunks. You should also make pages your default controller value as well.
See my answer for a similar question here from a few months back for example code and working code that I use in my Codeigniter CMS.
Here's the code I used in a recent project to achieve this. I borrowed it from somewhere; can't remember where.
function _remap($method)
{
$param_offset = 2;
// Default to index
if ( ! method_exists($this, $method))
{
// We need one more param
$param_offset = 1;
$method = 'index';
}
// Since all we get is $method, load up everything else in the URI
$params = array_slice($this->uri->rsegment_array(), $param_offset);
// Call the determined method with all params
call_user_func_array(array($this, $method), $params);
}
Then, my index function is where you would put your page function.

Categories