Welcome Guest, Not a member yet? Register   Sign In
Is it possible to use a service in a filter ?
#1

Hello,
I've create a filter to check the rights to access to the controllers. If the rights are not sufficient, I want to display a message. The messages are managed with a class, shared with Services: multiple messages can be generated before display.
The filter :
if (count($testRights) == 0) {
                    $message = service('MessagePpci');
                    $message->set(_("You don't have the necessary rights"), true);
                    return redirect()->to(site_url());
                }

When I display the content of the message, the text generated by the filter don't exists : it seems to me that the class Message was reinitialized or not shared.
Have you an idea?
Thanks,
Éric Q.
Reply
#2

PHP shares nothing between requests.
Yes, the message instance that has the error message will die at the end of the first request.
A new message instance will be created when the redirected request comes.

So you need
1. not to use redirect. If you show the message in the same request, no problem.
2. use Session or database or something to store the error message.
Reply
#3

(04-03-2024, 05:03 PM)kenjis Wrote: PHP shares nothing between requests.
Yes, the message instance that has the error message will die at the end of the first request.
A new message instance will be created when the redirected request comes.

So you need
1. not to use redirect. If you show the message in the same request, no problem.
2. use Session or database or something to store the error message.

Ok, thanks. I've not understand before that redirect() recreate a new call. I wrote this morning the storage of the messages into the session.
And a new related question: is it possible to chain multiple routes without use redirect()? For example, after a record into the database, I want to call the controller witch display the detail or go back to the list, and conserve the parameters (messages and others, as $_POST...) between each controller ?

Thanks,

Éric Q.
Reply
#4

Your mental model is not the same as the design of CodeIgniter.

Routing maps a route (URI path) to a controller.
A controller handles a HTTP request.
A controller returns a Response object or HTTP body string.

If you want to do many tasks, Models or Libraries do.
And you will build the response body string with the data from Models/Libraries
and views in a controller.

That is, calling another controller in a controller is not expected.
Reply
#5

(04-04-2024, 01:27 AM)kenjis Wrote: Your mental model is not the same as the design of CodeIgniter.

Routing maps a route (URI path) to a controller.
A controller handles a HTTP request.
A controller returns a Response object or HTTP body string.

If you want to do many tasks, Models or Libraries do.
And you will build the response body string with the data from Models/Libraries
and views in a controller.

That is, calling another controller in a controller is not expected.
Ok, I see it. I will call a class function in the return of the controller function, as :
return (myClass::myfunction());


I just have one interrogation: is it better to create a library class or to use the controller without another library? For data manipulation, I use models in all cases. Working with just controllers is simpler, but using libraries is perhaps more readable. I'm going to test it out.

Thanks.
Reply
#6

See https://codeigniter4.github.io/CodeIgnit...ontrollers
Reply




Theme © iAndrew 2016 - Forum software by © MyBB