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
Related
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');
Suppose i have a controller called user and one method myurl like below. The mylink method generate url list from database with pagination support.
class User extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function mylink($id){
/*
Here I want to delete the url(pseudo code :) )
//Check
IF GET request THEN
COLLECT the url ID and DELETE the url
DISPLAY success msg
ELSE
DISPLAY success msg
ENDIF
/*
This function will generate url and pagination
*/
//pagination settings
$config['base_url'].........
......
....
//End of pagination settings (I dont want to write whole text)
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
//call the model function to get the Url data
$data['urllist'] = $this->user_model->GetUrl($config["per_page"], $id);
/*
urllist contain url details and passed to view. where It will create delete url from respective url id like
http://example.com/mylink/del/1
http://example.com/mylink/del/1
http://example.com/mylink/del/1
http://example.com/mylink/del/1
*/
$this->load->view("layout/header");
$this->load->view("mylink",$data);
$this->load->view("layout/footer");
}
}
in my routes i have set
/*
this is for pagination like
http://example.com/mylink/1
http://example.com/mylink/1
http://example.com/mylink/1
*/
$route['mylink'] = 'user/mylink';
$route['mylink/(:num)'] = 'user/mylink/$1';
$route['mylink/del/(:num)'] = 'user/mylink/$1'; //***** Dont understand here how to set*****
But I dont understand how to set the delete url route and make it proper functional. Please help me with this. Thanks
This is my first question on here so please be kind! I'm setting up a Codeigniter program for the first time, and while I have my index view working, I can't get the individual view page to work. It returns a 404 page not found error.
I have code in my foreach for the index view pointing to where I think the url should be:
<a href=<?= site_url("kittens/view/" . $item->id)?>>Details</a>
This takes me to localhost:8888/index.php/kittens/view/1 if I click on the kitten with an id of 1. But I get a 404 error.
Here is my controller function for the view:
function view(){
$this->load->model("kittensmodel");
$this->load->view('_header');
$this->load->view('detailview', $data);
$data['kitten_item']= $this->kittensmodel->details();
}
And here is the function in my model:
function details(){
$id = $this->url->segment(3);
$this->db->select('*')
->from('kittens')
->where('id', $id);
$data = $this->db->get();
return $data->result();
}
And my detailview file just has this, to test it:
<?php
print_r($kitten_item);
?>
I've been playing around with the routes but haven't had any luck with it. Here's what I have right now:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['kittens/(:any)'] = 'kittencontroller/view/$1';
I'm new to php and codeigniter and the answer might be really simple, but I would really appreciate the help!
try to use this link
<a href=<?= site_url("kittens/".$item->id)?>>Details</a>
try it
<a href=<?= site_url("kittens/view?q=" . $item->id)?>>Details</a>
in controller
function view(){
$q = $this->input->get('q',True);
$this->load->model("kittensmodel");
$data['kitten_item']= $this->kittensmodel->details($q);
$this->load->view('_header');
$this->load->view('detailview', $data);
}
in model
function details($q){
$this->db->select('*')
->from('kittens')
->where('id', $q);
$data = $this->db->get();
return $data->result();
}
and in detailview
<?php
print_r($kitten_item);
?>
I think it is because your site uri link should be base_url and
Details
<a href=<?= base_url("kittens" .'/'. $this->url->segment(3))?>>Details</a>
Routes:
$route['kittens/(:any)'] = 'folder/controller-name/$1';
$route['kittens/(:any)'] = 'controller-name/$1';
Or
$route['kittens/(:any)'] = 'folder/controller-name/index/$1';
$route['kittens/(:any)'] = 'controller-name/index/$1';
Always make sure your link matches your routes that you set.
If your Controller is actually called kittenscontroller, then your route should be
$route['kittens/(:any)'] = 'kittenscontroller/view/$1';
CI will generate a 404 Page Not Found if your controller does not exist.
I noticed you use plural kittensxxxx everywhere else except in your controller used in the above route.
You Should always check this sort of thing by manually entering the actual URL to test it.
IF you are going to use that route, then your Links should become
Details
I am trying to make an small app so I have choosen Fat Free Framework. I need to show some messages based on successful or error. Suppose if I want to add an user then if successfully added show message that user has been added successfully or if not show error message that user cannot be added. I cannot figure it out. Here is my UsersController code
public function index(){
$user = new User($this->db);
$this->f3->set('users',$user->all());
//there should be a way to decide if its error message or success and after display,
//it shouldn't be displayed again for the same task.
//or may be it should be check in view file, I don't know where is the correct place
// to do it
$this->f3->set('page_head','User List');
$this->f3->set('view','users/list.htm');
}
public function create(){
if($this->f3->exists('POST.create')){
$user = new User($this->db);
$user->add();
//set session here to show in view file after redirect to list page
$this->f3->reroute('/users');
} else{
$this->f3->set('page_head','Create User');
$this->f3->set('view','users/create.htm');
}
}
My flash messages controller looks like this: https://github.com/ikkez/f3-flash/blob/master/lib/flash.php
To set a message i do:
if ($this->resource->updateProperty(array('_id = ?', $params['id']), 'published', true)) {
\Flash::instance()->addMessage('Your post was published. Hurray!', 'success');
} else {
\Flash::instance()->addMessage('This Post ID was not found', 'danger');
}
$f3->reroute('/admin/post');
To render the messages i include this template in my layout https://github.com/ikkez/fabulog/blob/master/app/ui/templates/alert.html which calls a function that dumps and clears all messages, so they will only be displayed once. You can also use the SESSION in a template token like {{#SESSION.flash}} and use it for <repeat> in the template.
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