Problem load view from controller in Codeigniter 3 - php

I want to load a main view from my controller in CodeIgniter 3.0, but instead of showing the view, the page shows the "Object of class CI_Loader could not be converted to string", and the view I want to show is on the top of my page not in the center of my page.
Here is the error screenshot :
This is my controller:
if($this->session->userdata('logged_in')){
$data['utama'] = $this->load->view('spvcoll/v_list');
$data['judul'] = 'List Assign';
$this->load->view('v_index',$data);
}else{
redirect('c_index');
}
Here I want to call the variable:
<?php echo $utama;?>

TRY This
$data['utama'] = $this->load->view('spvcoll/v_list', TRUE);
Instead of
$data['utama'] = $this->load->view('spvcoll/v_list');

Related

How to call a controller from a view in opencart?

I'm new to opencart. I want to create a custom theme and some custom controllers and models. I can't find any tutorials relative to this, but I tried to create a view along a controller. When I call that view from home or header view page, like $header (in home file) and $search (in header file), then it shows undefined variable.
My code looks like this. It's in controller (path is catalog\controller\common\test.php).
<?php
class ControllerCommonTest extends Controller{
public function index() {
if(file_exists(DIR_TEMPLATE.this->config->get('config_template').'/template/test/test.tpl')) {
$this->response->setOutput($this->render());
} else {
return $this->load->view('default/template/common/header.tpl');
}
}
}
?>
And my view is in \view\theme\MyTheme\template\common\test.tpl
<?php
echo "Test file";
?>
And in my home file, I call my controller like below...
<?php
echo $header;
echo $test;
echo $footer;
?>
When I run this it shows the below error:
Notice: Undefined variable: test in C:\xampp\htdocs\opencart\catalog\view\theme\MyCustome\template\common\home.tpl on line 4
So, please provide any tutorial links and any examples for developing a custom module in opencart.
Thanks in advance.
To display test module tpl i.e. test.tpl on home page, You have load test controller on home controller. Please add following code in catalog/controller/common/home.php
add this code
$data['test'] = $this->load->controller('common/test');
After
$data['header'] = $this->load->controller('common/header');

Codeigniter : How can remove any error message for user

I have problem in link of my script by Codeigniter.
I have this URL :-
localhost/index.php/details/1
Its get me all details of product, by ID , But when i add any character in the end of link like this :
localhost/index.php/details/ggg
The user see some error message.
How can save my link, And hidden this message.
Method 01
in config/routs.php
$route['404_override'] = 'Controller name';//this to avoid 404 Error
Method 02
in your controller check empty
$data['product'] = $this->Model_Name->get_product($id);
if(empty( $data['product']))
{
$this->empty_result();
}
else
{
//load relevent view
}
In controller at top create function call empty_result()
public function empty_result()
{
$this->load->view('template/header');
$this->load->view('template/right_sidebar');
$this->load->view('template/NothingFound',$data);//create a page to show empty or Nothing found
$this->load->view('template/foot');
}
In Model
public function get_product($id)
{
$query = $this->db->query("SELECT * FROM product WHERE id ='$id'");
$result = $query->result_array();
return $result;
}
In view (template/NothingFound.php)
create file call NothingFound.php
in that
<h1>No thing found title</h1>
<!--customize the view as you want. Add some images-->
EDIT 01
Method 03
Goto error/error_404.php
Edit the error message to display in your styles
you can easily do this using 404 page .
in your routes.php file find the line :
$route['404_override'] = '';
in single quotation add contrller/ action method . in this method add view 404 page design . then when any error occured your user see the 404 error page

How to structure a dynamic website with Codeigniter

I have some experience with php, but I just recently started learning Codeigniter. I used to have a website with a fixed navigation pane and sidebar, but the main section of the site loaded dynamically based on a Get variable. It was basically
include head.php
include navbar.php
include sidebar.php
include the page requested from the get variable (home, about, contact, etc.)
include footer.php
I liked this because the entire site did not have to reload when the user navigated from page to page.
I can't figure out how do this with Codeiginiter. Should I be using a controller for each page or one controller with a function for each page? Do anyone know of a good tutorial that does something similar? All the tutorials I've seen reload the entire site for every page.
Edit: Essentially I want to do this but with Codeigniter
Since it looks like you want relatively static content, but loaded dynamically you can do with one controller and (maybe) one method in the controller.
To do it with one method, do this in the welcome controller:
public page( $page_id ){
// views/header.php
$this->load->view( "header" );
if( $page_id = "about" ){
$this->load->view("about"); // views/about.php
}
else if( $page_id = "contact" ){
$this->load->view("contact"); // views/contact.php
}
// views/footer.php
$this->load->view("footer");
}
This takes a single get variable and figures out what page to load in between the header and footer.
This way www.yoursite.com/page/about will load the about page, www.yoursite.com/page/contact will load the contact page
Now, if you want to get rid of the /page part, you need to do some URL rerouting in application/config/routes.php
Alternatively you could use several methods in one controller:
public about( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "about" );
// views/footer.php
$this->load->view("footer");
}
public contact( ){
// views/header.php
$this->load->view( "header" );
$this->load->view( "contact" );
// views/footer.php
$this->load->view("footer");
}
Now your URLs look nicer without routing, but you have to load the header/footer for every page.
do you really like to copy/paste many $this->load->view() to any controller function?
It's a spaghetti code. You can try next: for example we have main.php controller as default controller. This main controller contain main function:
public function index()
{
ob_start();
$this->load->model('mainmodel');
$data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
foreach ($data->result_array() as $row) {
$this->load->module($row['block_name']);
$this->name = new $row['block_name'];
$this->name->index();
}
ob_end_flush();
}
So, each other controller also have index() function which can dispatch actions depends on url segments, prepare params etc.
Footer controller as example (I use Smarty as template engine):
public function index()
{
$this->mysmarty->assign('year', date("Y"));
$this->mysmarty->view('footer');
return true;
}
Content controller will have:
public function index()
{
$name = $this->uri->segment(1, 'index');
$act = $this->uri->segment(2, 'index');
$this->load->module($name);
$this->name = new $name;
$pageData = $this->name->_show($act);
if ($pageData)
{
$this->mysmarty->assign($name, $pageData);
}
$this->mysmarty->view($name);
}
Thats mean what if you want to show http://site.name/page/contactus , we do next:
1) main.php start cycle by needed blocks
2) firstly we show header.tpl by header controller
3) then we show menu
4) then we call content controller which parse url, found what he should call _show() function in Page controller and pass action='contactus' to it. _show() function can contain some switch/case construction which show templates depends of action name (contactus.tpl in this case)
5) in the end we show footer template
In such case we have flexible structure. All controllers should have index() functions and all controllers who can be called in content should have _show($act) function. Thats all.
In codeIgniter you can do that like this, you can load different views at the same time from your controller. for example:
for example in your navbar view you have a Contacts button in your menu that would look like this:
<a href='contacts'>Contacts</a>
In your controller:
public function contacts()
{
$this->load->view('header');
$this->load->view('navbar');
$this->load->view('sidebar');
$this->load->view('contacts_view');
$this->load->view('footer');
}
So we're assuming here that you have the following views already that is ready to be loaded (header.php, navbar.php, sidebar.php, contacts_view.php, footer.php).
UPDATE:
you don't need to have $_GET[] request, just provide the method name from your controller in the <a> anchor tag
in codeigniter i using template
first make template file in one folder with header.php, navbar.php, etc.
example : template.php
<?php
echo $this->load->view('header'); //load header
echo $this->load->view('navbar');//load navbar
echo $this->load->view('sidebar');//load sidebar
echo $this->load->view($body); //load dynamic content
echo $this->load->view('footer');//load footer
?>
second in controller
function index( ){
$data['body'] = 'home'; // cal your content
$this->load->view('template', $data);
}

Going to specific part of a webpage using anchor name attribute by loading view in codeigniter

Using <a name="form"></a> and then going to that position by Click here
In code-igniter, I'm trying to load a view and a specific part of that view ie., <a name="form"></a>. It can be easily done by redirecting to that page using the #name but that clears up the form validation errors.
if($this->form_validation->run() == false)
{
$this->add();
}
where add() is the function which loads the view.
public function add()
{
$data['main_content'] = "add_view";
$data['css_code'] = "add_view.css";
$this->load->view('template', $data);
}
Any thoughts? Thanks in advance!
Ah! Found the answer at last.
You just have to add the anchor to the URI in form_open()
<?php echo form_open('controller/function#anchor'); ?>
Reference Codeigniter Forum

CakePHP controller/action question with http://mysite.com/mycontroller/absentaction

Suppose someone hits in url http://mysite.com/comments/view/13
But that absentaction is not present in comments controller.
Then it gets normal error like that =>
Error: The action view is not defined in controller CommentsController
Error: Create CommentsController::view() in file: app/controllers/comments_controller.php.
<?php
class CommentsController extends AppController {
var $name = 'Comments';
function view() {
}
}
?>
Notice: If you want to customize this error message, create app/views/errors/missing_action.ctp
What i'm trying to do is that if someone hits url http://mysite.com/comments/view/13 and if the action is not present then it will redirect to http://mysite.com/.
How can i do this for unknown/absent action?
This trick is actually working pretty well.
You need to create a file app/app_error.php
<?php
class AppError extends ErrorHandler {
public function error404($params){
extract($params);
if(!isset($url)){
$url = $action;
}
if(!isset($message)){
$message ="";
}
if(!isset($base)){
$base = "";
}
$this->controller->redirect(array('controller'=>'pages','action'=>'home'));
//Or the page you want...
}
}
?>
How does it work?
It actually override the error404() function from the ErrorHandler and redirect the user whith $this->controller->redict();
Notice at the bottom of the error message, it says you can customize it by creating app/views/errors/missing_action.ctp. So all you need to do is create that .ctp file and include a redirect in it like this:
<?php
header( 'Location: http://mysite.com' ) ;
?>
It says it right in the error...
create app/views/errors/missing_action.ctp
And that's what you should do...
Try using a header in the missing_action.ctp to redirect to where you want the page to go.
You can either customise app/views/errors/missing_action.ctp or you can turn off debugging in app/config/core.php

Categories