Jump to content

New Rest Helper for Processwire


clsource
 Share

Recommended Posts

Hello folks

I have updated the code in my rest helper.

Since It was created nearly 2 years ago! 

Now its much easier to create rest endpoints in Processwire

You can download the code here.

https://github.com/NinjasCL/pw-rest


This is and example simple login code.

$response = new Response();

$params = Request::params();

if (!Request::isPost()) {

	$response->setError(MethodNotAllowed::error());

} else {

	$username = $params['username'];
	$password = $params['password'];

	if ((!isset($username) || $username == '') ||
		(!isset($password) || $password == '')) {

		$response->setError(Login\Errors\InvalidCredentials::error());

	} else  {

		if ($username == 'hello' && $password == 'world') {

				$response->output['data']['name'] = 'Tony';
				$response->output['data']['lastname'] = 'Stark';
				$response->output['data']['job'] = 'Ironman';

		} else {

			$response->setError(Login\Errors\InvalidCredentials::error());
		}
	}
}
$response->render();

Will render something similar to

{
  "data": {
    "name": "Tony",
    "lastname": "Stark",
    "job": "Ironman"
  }
}

Any questions or comments are welcome :D

  • Like 9
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...