GithubHelp home page GithubHelp logo

li3_jbuilder's Introduction

Adding JSON Responses to an Application Build Status

To demonstrate the befinits of this libary, we’ll use a simple application. This app has many discussions and we’d like to add a JSON representation for each discussion that we can fetch by appending .json to the discussion’s URL. If we try this we’ll see the JSON representation of the discussion.

Customizing The Response

The JSON returned includes all of the discussion’s attributes but what if we want to customize it? This is where things can start to get ugly. We can call to('json') on the discussion and customize what’s returned. Let’s say that we want the id, subject and content fields from the discussion along with its author and the same fields from the discussion’s messages.

public function show() {
	$discussion = Discussions::find($this->request->id);

	if (!$discussion) {
		return $this->redirect('/discussions');
	}

	if ($this->request->is('json')) {
		// we don't want to expose this
		unset($discussion->created_at);

		// additional data comes from associated Author and Comments records.
		$discussion->author = [
			'name' => 'Mehdi Lahmam B.',
			'id' => '1234'
			];
		$discussion->comments = [
			['content' => 'w00t', 'author' => 'John Doe'],
			['content' => 'This is sexy', 'author' => 'Jimmy']
		];

		return compact('discussion');
	}

	return compact('discussion');
}

We can try this by reloading the page again. When we do we see the customized JSON response including the associated Author and Comments records.

This works, but the code we’ve used isn’t very pretty.

A better way

Coming soon !

Credits

Loosely based on the original, and a PHP port

li3_jbuilder's People

Contributors

mehlah avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.