Welcome Guest, Not a member yet? Register   Sign In
Customized 404 Error extending class
#1

[eluser]Jorgeska[/eluser]
Hello.
I'm starting with CI 2.1.3. I am trying to compose my custom error 404 page. These are the steps I followed (just in case I forgotten someone):

1 - route.php

Code:
$route['default_controller'] = 'pages';
$route['404_override'] = 'errors/error404'; # Mi error 404 page
$route['pages'] = 'pages';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['blog/comments'] = 'blog/comments';
$route['blog'] = 'blog';

2 - errors.php

Code:
<?php
class Errors extends CI_Controller {

    public function index()
    {
        $data['title'] = ucfirst('home'); // Capitalize the first letter
    
        $this->load->view('templates/header', $data);
        $this->load->view('pages/home', $data);
        $this->load->view('templates/footer', $data);
    }
    
    public function error404()
    {
     echo '<br>MIERROR<br>';
     show_404();
    }
}

3 - MY_Exceptions.php (Prefix 'MY_' as in config/config.php)

Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends CI_Exceptions {
    
    public function __construct()
    {
        parent::__construct();
    }
    
    function show_404($page = '', $log_error = TRUE)
    {
        
        $heading = "404 Page Not Found";
        $message = "From MY_Exception class : The page you requested was not found.";

        // By default we log this, but allow a dev to skip it
        if ($log_error)
        {
            log_message('error', '404 Page Not Found --&gt; '.$page);
        }

        echo $this->show_error($heading, $message, 'error_404', 404);
        exit;
    }
}

4 - I put http://www.example.com/wrong on the browser and it takes me to the following page ...

Code:
MIERROR
404 Page Not Found
The page you requested was not found.

... where, as you can see, there is MIERROR but no MIEXCEPTIONCLASS. It is ignoring my MY_Exceptions. Does anybody know why? Does my folder structure has anything to be with the problem? This is (short version) the following:

Code:
www.example.com
----aplication
--------cache
--------ci001_tutorial (aplication #1)
------------config
------------controllers
----------------blog.php
----------------errors.php
----------------news.php
----------------pages.php
----------------welcome.php
------------errors
------------libraries
------------models
------------views
--------ci002_otra_aplicacion (aplication #2)
--------core
----------------MY_Exceptions.php
--------helpers
--------hooks
--------language
--------logs
--------third_party
----cgi-bin
----codeigniter (=system)
----htdocs
--------index.php
----logs

This is the tutorial recomended folder structure for more than one aplication in the same codeigniter project.
I have unsuccessfully tried too putting MY_Exceptions.php into ci001_tutorial/libraries.
What am I doing wrong?
Thank you everybody!

#2

[eluser]CroNiX[/eluser]
I haven't used multiple applications like you are there, but each app might be expecting it to be in it's own core dir.

Like
--------ci001_tutorial/core/MY_Exceptions.php

and
--------ci002_otra_aplicacion/core/MY_Exceptions.
#3

[eluser]Jorgeska[/eluser]
I know. But the structure I have put is the way CI documentation says it goes. It should work, according to the tutorial.
It makes sense if you pretend to re-use code among applications as much as posible, but I have found out that it doesn't work at least for core extensions.
I must tell that my MY_Exceptions works successfully in the simplified folder structure, the standard one, I mean the one for a single project. So I came back to this.
Anyway I would prefer to start working in a multi-project structure, I mean a middle point between each CI app in a whole CI structure and all the apps mixed in an only one CI structure.
I think the profit of this is the possibility of easy code re-using eventhough application are clearly separated.

Somebody in the Forum said that he usually put this folder levels for a multiproject environment ...

Code:
www.example.com
----aplication_app1
--------cache
--------config
--------controllers
--------errors
--------libraries
--------models
--------views
--------core
--------helpers
--------hooks
--------language
--------logs
--------third_party
----aplication_app2
--------cache
--------config
--------controllers
--------errors
--------libraries
--------models
--------views
--------core
--------helpers
--------hooks
--------language
--------logs
--------third_party
----system
----htdocs_app1
--------index.php
----htdocs_app2
--------index.php

Probably it works, but that way we can't share much code, can we? OK, it saves a duplicated 'system', but just this.
What's your prefered folder structure thinking of multiple applications?
#4

[eluser]CroNiX[/eluser]
If I have to do this I just create a new folder in the /application/controllers dir for code separation. I guess it also has a lot to do with terminology. I don't really consider different things accessing common code necessarily as "separate apps". If they share a lot of common code, I consider it the same app but maybe a different module, like an administrative module, in which case I'd do something like:
/application/controllers/admin
/application/controllers/admin/controller1.php
/application/controllers/admin/controller2.php

/application/models/admin/admin_model.php (if it shares NO code with common models, if not it just goes in /application/models)
/application/views/admin/view1.php
etc.

I realize this isn't as modular as some people want (drag/drop a "module" folder with self contained controllers/models/views/etc) into an existing project, but I don't write code for the masses where they'd need to do something like that and this structure works perfectly fine for my clients needs. There is HVMC (not part of CI core) that offers this kind of modularity within CI where everything is self contained, but I haven't had the need to try them so I can't really advise there.

If I truly create a separate application, it gets it's own virtual host and the only thing it shares with other apps is the system folder set in the index.php.




Theme © iAndrew 2016 - Forum software by © MyBB