-
Notifications
You must be signed in to change notification settings - Fork 49
Association Controller
JP Barbosa edited this page Mar 14, 2016
·
4 revisions
php artisan make:controller AuthorsController
nano app/Http/Controllers/AuthorsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Author;
class AuthorsController extends Controller
{
public function index()
{
return Author::all();
}
public function store(Request $request)
{
return Author::create($request->all());
}
public function show(Author $author)
{
return $author;
}
public function update(Request $request, Author $author)
{
$author->update($request->all());
return $author;
}
public function destroy(Author $author)
{
return (string) $author->delete();
}
}
nano app/Http/routes.php
Route::resource('authors', 'AuthorsController');
php artisan serve
curl -H "Accept: application/json" \
http://localhost:8000/authors \
--data "name=Author Name" \
--data "[email protected]"
{"name":"Author Name","email":"[email protected]"...}
curl -H "Accept: application/json" \
http://localhost:8000/authors
php artisan make:request AuthorRequest
nano app/Http/Requests/AuthorRequest.php
...
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email'
];
}
...
nano app/Http/Controllers/AuthorsController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\AuthorRequest;
use App\Http\Controllers\Controller;
use App\Author;
class AuthorsController extends Controller
...
public function store(AuthorRequest $request)
...
public function update(AuthorRequest $request, Author $author)
...
}
curl -H "Accept: application/json" \
http://localhost:8000/authors \
--data "name=Author Name" \
--data "email=invalid_email_address"
{"email":["The email must be a valid email address."]}
curl -H "Accept: application/json" \
http://localhost:8000/authors \
--data "name=Author Name" \
--data "[email protected]"
{"name":"Author Name","email":"[email protected]"...}
nano app/Http/Requests/ArticleRequest.php
...
public function rules()
{
return [
'title' => 'required|min:3',
'content' => 'required',
'author_id' => 'required',
];
}
...
curl -H "Accept: application/json" \
http://localhost:8000/articles \
--data "title=Article Title" \
--data "content=Article Content"
{"author_id":["The author id field is required."]}
curl -H "Accept: application/json" \
http://localhost:8000/articles \
--data "title=My Article Title" \
--data "content=My Article Content" \
--data "author_id=1"
{"title":"My Article Title","content":"My Article Content","author_id":"1"...}
git add .
git commit -m "Add authors associations controller"
Next step: Association Views
- Setup
- Basic CRUD
- Validation
- Views
- Association
- Association Controller
- Association Views
- Basic Template
- Bootstrap
- Bootstrap CRUD
- Alerts
- Welcome Page
- Ajax CRUD
- Send Email
- Send Email Views
- Jobs Queue
- Captcha
- Async External Content
- Cached External Content
- Tests Setup
- Functional Tests
- Acceptance Tests
- Continuous Integration
- Deploy with Heroku
- Deploy with Forge
- Update README