简体   繁体   中英

Getting user friendly messages back to the View

This is a summary of how a user would login in my application(up to validating the data):

  1. Submit the login form.

  2. Router figures out where the user is trying to go.

  3. The correct Controller is initialized.

  4. The RecognitionService is started and the login() method is run.

  5. Within the login() method I set the supplied username and password in my User domain object.

  6. My User domain object will check if the username and password are valid by checking their type, length, format etc.

All this is good except for when the entered username contains invalid characters and then getting the error message from my domain object back to the View in a neat and tidy way.

At the moment I am having my domain objects return the message (well really the array key to the message) to the RecognitionService which then holds onto the message array key and then in the View I retrieve the message array key from the RecognitionService and use it to display the correct message but I have a feeling there is a better way of doing it, any ideas?

Thanks.

The controller and current view should share the same factory for instantiating services, which ensures that each service is initialized only once.

From controller:

public function postLogin( $request )
{
    $recognition = $this->serviceFactory->create('Recognition');
    $recognition->authenticate( $request->getParameter('username'),
                                $request->getParameter('password') );
}

From view:

public function login()
{
    $recognition = $this->serviceFactory->create('Recognition');
    if ( $recognition->hasAuthenticationError() )
    {
        header('Location: /login');
    }

    if ( $recognition->hasChangedState() ) //was POST, PUT or DELETE call
    {
        header('Location: ./');
    }
}

public function render()
{
    // .. make the html/json/whatever
}

Something like this .. that's my 2 cents. Honestly, how you implement the view is entirely up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM