If I use this code the page displays:
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model("m_welcome", "mod");
}
public function index()
{
$this->load->view("test.php", array());
//exit;
}
}
if I comment the //exit; it shows all the page. If I don't comment it, the page doesn't display anything. It's very weird.
You are cancelling execution early - it's not an error, but the expected behaviour in CI.
If you exit at that point, you are stopping the execution from sending output to the browser. The response from what ever is in the route method will not be sent to the parent method handling the request & response.
Related
I have an Item controller where I call the destroy function when I hit the submit button.
public function destroy($id)
{
items::destroy($id);
return redirect()->route('items.index');
}
This is the corresponding method for the route named items.index:
public function index()
{
$items = items::orderBy('category','asc')
->orderBy('partnumber','asc')
->get();
return view('items.index')->with('items', $items);
}
I am redirected to this index view but the url remains the same. Can anyone explain to me why the URL is not changing?
Here is a screen shot:
I put the line
$route['404_override'] = 'my404';
in the routes.php file, and I made a controller with a name my404:
<?php
include 'page.php';
class My404 extends Page {
private $page;
public function __construct() {
$page = new stdClass();
parent::__construct();
}
public function index() {
// $this->page->page_title = "Page not found!";
$this->page_view($this->load->view('my404', $this->page, true));
}
}
?>
and I made a View with a name of my404
It is working fine when I make a syntax error in the controller name, but it won't work if I write a wrong method and a Server error will occur instead of the customized 404 page, have I missed something in the routes.php file ?
From Codeigniter user guide:
This route indicates which controller class should be loaded if the requested controller is not found.
Apparently that rout will only work when controller isn't found.
you can instead override the default error_404 page at 'application/errors/error_404.php'.
Is it a bug or a feature?
I have two controllers IndexController and TestController.
The first one looks like this:
class IndexController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$products = $this->basket->get('products', []);
$products[] = uniqid('index.index');
$this->basket->set('products', $products);
}
public function testAction()
{
var_dump($this->basket->products);
}
}
I just save an array in a session with indexAction and show that data in testAction.
For the first request output for index/test is an empty array. But the second and all following requests add one new element. If I comment line $this->basket->set('products', $products); then the next request to index/test will add another value to array but after that extra pushing to array does not happen.
Then I add
class TestController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$products = $this->basket->get('products', []);
$products[] = uniqid('test.index');
$this->basket->set('products', $products);
die();
}
public function testAction()
{
var_dump($this->basket->products);
die();
}
}
But for request test/test I again get changing in session variable.
Looks like that route index/index always executes before any other routes. Is it a feature, bug or some sort of misconfiguration? I use standard configuration for multimodule application from official documentation.
This is more than likely because your browser is hitting /favicon.ico in the background. I've been stung by this a number of times across different frameworks.
Try putting a favicon.ico file (or a rule to block that path) and see if the problem persists.
Here's my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('user_model');
}
public function index() {
redirect('user/login');
}
public function login() {
$this->load->library('form_validation', '', 'fv');
var_dump($this);
$this->fv->set_rules('nick_name', 'Nick Name', 'trim|required|max_length[12]|min_length[4]');
$this->fv->set_rules('password', 'Password', 'trim|required|md5');
}
}
?>
I was basically writing a login controller. In the login function, after this I load the view, etc. But up till here, I am getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: User::$fv
Filename: controllers/user.php
Line Number: 23
I have commented out everything else in the controller effectively to what I have pasted above. I don't get where the problem is. Thanks.
[EDIT]
On var dumping the $this object, I found an even more unusual thing. The object "fv" was not defined inside the $this object but rather under the $this->user_model object. So isset($this->fv) was returning false and isset($this->user_model->fv) was returning true. And yes I tried $this->load->library('form_validation') instead of naming it "fv", results remained the same.
Your controller name should named as user.php & it should located inside the controllers folder.
Try CI recommended coding styles, such as,
$this->load->library('form_validation');
$this->form_validation->set_rules(); etc..
i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404
my controller error.php :
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
echo "error bro";
}
}
when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”
i try change line in method error_404() become the code below,
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
$data['title'] = "404 Page Not Found";
$data['body'] = $CI->load->view('web/404','',true);
$CI->load->view('web/web_page',$data);
}
}
but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page.
the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??
thanks
Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?
But why not just set the $route['404_override'] variable?
Just set a proper route in your config/routes.php:
$route['404_override'] = 'your_controller/show_404';
And the corresponding controller would look like that:
class Your_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
function show_404() {
$this->output->set_header("HTTP/1.1 404 Not Found");
$this->load->view ('common/errors/404'); //wherever your view is
}
}
That should do the trick for you.