Welcome Guest, Not a member yet? Register   Sign In
loading my model to load_class
#4

(This post was last modified: 11-01-2017, 01:11 AM by Vitaly83. Edit Reason: RE: )

(10-31-2017, 08:12 AM)dave friend Wrote: A workaround you can use is to name your file  and class name using the subclass_prefix.

I use to add prefix only for extended classes (like, libraries, core, helpers and etc.) and don't add prefix otherwise. Should I add prefix always?


(10-31-2017, 09:18 AM)Narf Wrote: This is very common, both here and on StackOverflow ... People don't ask how to do a thing; they decide how they do it (usually the first idea that comes to mind) and then ask how to make that idea work, when it was never a good idea in the first place.

Sorry, my mistake, you are absolutely right.

Let me tell why I decided to use this function.

I extended the Exception library to send emails on errors. Recipient(s) is(are) not a constant and can be get from a module (that I need to load). To load this module (and email library too)  I need to get an CI instance ($CI =& get_instace()).

Next, the CSRF Protection is turned on.
In Input library constructor we can see this code:

PHP Code:
    public function __construct()
    {
        ...
        // CSRF Protection check
        if ($this->_enable_csrf === TRUE && ! is_cli())
        {
            $this->security->csrf_verify();
        }
        ...
    

Then the Security library's method csrf_verify:
PHP Code:
    public function csrf_verify()
    {
        ...
        if ($valid !== TRUE)
        {
            $this->csrf_show_error();
        }
        ...
    

The csrf_show_error method just calls the show_error method of the Exception library.

As I said before, I extended the Exception library and get $CI instance on every error.

On POST requests with wrong csrf data I get such message:

Quote:<br />
<b>Fatal error</b>:  Class 'CI_Controller' not found in
<b>/var/www/html/site/system/core/CodeIgniter.php</b> on line
<b>369</b>
<br />
<br />
<b>Fatal error</b>:  Class 'CI_Controller' not found in
<b>/var/www/html/site/system/core/CodeIgniter.php</b> on line
<b>369</b>
<br />

I just need to be notifed about errors. If you know how to do this another way it would be great.

That's why I decided to not to use CI instance and load libraries and modules with load_class method.

P.S. Most of these POST requests from outside (not initiated by system). And I need the CSRF Protection to be turned on.

UPD:
Here is MY_Exceptions class:
PHP Code:
class MY_Exceptions extends CI_Exceptions
{
    /**
     * @param string $subject
     * @param string $message
     */
    private function sendAdminEmail($subject$message)
    {
        $CI =& get_instance();
        $CI->load->library('Employees');
        $admin $CI->Employees->getAdmin();
        $CI->load->library('email');
        $CI->email->clear(true);
        $CI->email->from('[email protected]''Error notifier');
        $CI->email->to($admin['email']);
        $CI->email->subject($subject);
        $CI->email->message($message);
        $CI->email->send();
    }

    /**
     * @param string $heading
     * @param string|\string[] $message
     * @param string $template
     * @param int $status_code
     *
     * @return string
     */
    public function show_error($heading$message$template 'error_general'$status_code 500)
    {
        ...
        $this->sendAdminEmail(
            sprintf('Exception: %s'$heading),
            $buffer
        
);
        ...
    }

    /**
     * @param Exception $exception
     */
    public function show_exception($exception)
    {
        ...
        $this->sendAdminEmail(
            sprintf('Exception #%d: %s'$exception->getCode(), $exception->getMessage()),
            $buffer
        
);
        ...
    }

    /**
     * @param int $severity
     * @param string $message
     * @param string $filepath
     * @param int $line
     */
    public function show_php_error($severity$message$filepath$line)
    {
        ...
        $this->sendAdminEmail(
            'PHP Exception',
            $buffer
        
);
        ...
    }



I overwrited standart Exceptions methods to just add call of the sendAdminEmail method. Nothing else.
Reply


Messages In This Thread
loading my model to load_class - by Vitaly83 - 10-31-2017, 06:57 AM
RE: loading my model to load_class - by Narf - 10-31-2017, 09:18 AM
RE: loading my model to load_class - by Vitaly83 - 10-31-2017, 10:31 PM
RE: loading my model to load_class - by Narf - 11-01-2017, 05:05 AM
RE: loading my model to load_class - by Vitaly83 - 11-01-2017, 05:51 AM
RE: loading my model to load_class - by Vitaly83 - 11-02-2017, 02:30 AM
RE: loading my model to load_class - by Narf - 11-02-2017, 03:38 AM
RE: loading my model to load_class - by Vitaly83 - 11-02-2017, 04:12 AM
RE: loading my model to load_class - by Narf - 11-02-2017, 06:15 AM
RE: loading my model to load_class - by Vitaly83 - 11-02-2017, 07:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB