Welcome Guest, Not a member yet? Register   Sign In
Not sure to migrate to CI4
#11

(05-14-2020, 08:13 AM)imabot Wrote: A simple example: some pages are restricted to some users, depending on the user rights. I need to load the page data before checking  if the current user is allowed to visit the page. Filters are not great because I have to load and process the page data twice (once in the filter and once in the controller). Redirecting from models / libraries / helpers is no longer possible. So it must be done in controllers. The problem is that my controllers becomes more and more complicated. Even worse, I can't split my code into functions, because redirection in CI4 is based on return, so I can't redirect from a private method in my controller or BaseController. Do you get it?

And what about throwing an exception? If you encounter any problem in your model, throw an exception, the controller catch it and redirect with the error message.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#12

(This post was last modified: 05-18-2020, 02:17 AM by imabot.)

Thank you for your feedback. I know about Symfony but at the beginning, when I selected a framework, I picked CodeIgniter for its simplicity ... so your arguments do not really convinced me.

Quote:And what about throwing an exception? If you encounter any problem in your model, throw an exception, the controller catch it and redirect with the error message.

Here is a good idea ... I'll have a look at it. I should have think at it earlier. Thank you.

Hum ... can I throw an exception and manage the error by myself with a custom error message and redirection?
Reply
#13

(05-18-2020, 02:09 AM)imabot Wrote:
Quote:And what about throwing an exception? If you encounter any problem in your model, throw an exception, the controller catch it and redirect with the error message.

Here is a good idea ... I'll have a look at it. I should have think at it earlier. Thank you.

Hum ... can I throw an exception and manage the error by myself with a custom error message and redirection?

Yes. You can even create custom exception. But if you want to keep it simple, just throw new Exception(“Your custom error message...”); and in your controller catch the exception, get the message from the Exception object and redirect with that message.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#14

(This post was last modified: 05-18-2020, 01:16 PM by bivanbi.)

I found throwing a generic exception made my life harder in the long run. I definitely want to differentiate, for instance, a real generic error like backend DB unreachable vs. user is not logged in / not having the required rights to perform action etc. When I catch the exception, I find it much cleaner with different Exception classes:

Code:
class MyController extends BaseController {
    public function main()
    {
        try {
            doSomething();
        } catch (\Exception $e) {
            myCustomErrorHandling($e);
        }
    }

    private function doSomething()
    {
        // require user to be logged in and have permission to access this function
        requireRole(USER_ROLE_ADMIN);
        // do the actual work
    }
}


class BaseController {
    protected function myCustomErrorHandling($e)
    {
        if ($e instanceof UserNotLoggedInException) {
            // redirect to login page
        } else if ($e instanceof AccessDeniedException {
            // let the user know he/she has no access to the given function
        } else {
            // redirect to a generic error page
        }
    }

    protected function requireRole(string $role)
    {
        if (! isLoggedIn()) {
           throw new UserNotLoggedInException("Must be logged in");
        }

        if (! hasRole($role)) {
           throw new AccessDeniedException("No permission to access");
        }
    }
}
Reply
#15

As a very new Ci4 and having never experienced ci3, I am impressed with some of the intended functions of Ci4 but am very disappointed in the detail of the documentation, examples of code usage, and the knowledge base in the support forum.

Yes, there are a handful of experienced and very helpful forum members/admins, but I am finding support, advise or even a gentle nudge in the right direction difficult to come by. And, as I am very new to Ci I'm finding some of the simpler hurdles I have to overcome difficult as I cannot get advice on whether my code is wrong, the documentation is wrong, my interpretation of the documentation is wrong, or whether I have encountered one of a number of bugs that seem to be present in the framework.

I have a cumbersome project (my own project, not a customer project) already developed and working in a procedural style (very difficult to maintain and troubleshoot) so need to move to a stable framework. But each time I take a step forward with Ci4 I also seem to take two steps back.

I will give it another week or so, but it seems Ci4 is a very new product, and some of the points mentioned above are making my project migration a bit of a living nightmare. That said I really want to make strides with Ci4 as other frameworks like Symfony and Laraval seem so abstract.
Reply
#16

(This post was last modified: 06-13-2020, 08:24 AM by jreklund.)

Hi 68thorby68, I got your email and see that you have raised concerns in the forum as well. I don't want to directly answer emails, as I don't want to disclose my personal email.

I have written an answer to all your question, so I can post it here (in this thread) or you need to enable personal messages in this forum.

https://forum.codeigniter.com/usercp.php?action=options "Receive private messages from other users."

Regards
Johan
Reply




Theme © iAndrew 2016 - Forum software by © MyBB