Phone book code snippets

In SiteContoller::beforeWebMethod(), I can’t help thinking that this:




  if (! Yii::app()->user->isGuest || in_array($service->methodName, $safeMethods))



is easier than this:




  $pattern='/^('.implode('|',$safeMethods).')$/i';

  if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))



Also, would it not make more sense to throw a SoapFault on login failure?




  public function login($username, $password) {

    $identity = new UserIdentity($username, $password);

    if ($identity->authenticate())

      Yii::app()->user->login($identity);

    else

      throw new CException('Login attempt failed');

  }



And, finally, if someone is interested in a SOAP client test script to run outside of Yii:




<?php


class Contact {

  public $id;

}


$wsdlUrl = 'http://your_host_here/yii/demos/phonebook/index.php?r=site/phonebook';

$client = new SoapClient($wsdlUrl);


echo "Logging in...\n";

try {

  $client->login('demo','demo');

} catch (SoapFault $e) {

  echo 'Caught SOAP fault: '. $e->getMessage(); exit(1);

}


echo "fetching all contacts\n";

print_r($client->getContacts());


echo "\ninserting a new contact...\n\n\n";

$contact = new Contact;

$contact->name = 'Mr Soap Client';

$contact->phone = '123-999-1234';

try {

  $client->saveContact($contact);

} catch (SoapFault $e) {

  echo 'Caught SOAP fault: '. $e->getMessage(); exit(1);

}


echo "done\n\n";


echo "fetching all contacts\n";



Yeah, your version is easier for simple cases.

The reason we are using regexp is when there are many protected methods who follow similar patterns, it would be easier.

Throwing SoapFault is fine if the client is PHP. For other clients such as Flex, SoapFault may be difficult to deal with.

Ah well, different strokes. I’d rip out the regex in a refactor without blinking. I guess I did!

I don’t follow this either. If Flex isn’t following standards, then that’s Flex’s problem. Ref:

http://www.w3.org/TR/soap12-part1/#faultcodes