Building a Support Ticket in laravel lucid architecture part 4
What Will I Learn?
For this installment, we are going to learn the following
- Creating a validator
- Creating a validateTicketInputJob
- We are also going to learn how to create a repository.
Requirements
Write here a bullet list of the requirements for the user in order to follow this tutorial.
- Download php 7.2.1
- Download Composer.exe
- Download xampp or wamp for windows.
Difficulty
- Intermediate
Introduction
In lucid architecture we make our controllers as thin as possible. The idea is to get the controller to produce just one task at different functions.
In series 1 we talked about repositories, repositories are the only way we connect to our models. This is called decoupling of the data source from the actual mechanism.
What is our task today.
Today, we are going to introduce jobs and repository. we are going to work on the store ticket feature and all its components.
So lets start
I guess by now, you know how it works, lets initiate the route for the mechanism
Route::post('new_ticket', 'TicketController@store');
fine we have thee route, let head over to the controller we generated in our previous post and look for the store function and add the code below
return $this->serve(StoreTicketFeature::class);
remember to add or rather include the StoreTicketFeature at the top by adding this
use App\Services\Web\Features\StoreTicketFeature;
One let generate the feature from command, if u still can't look at my previous tutorial
on the CLI enter
lucid make:feature StoreTicket web
Now lets analyze the feature, we are going to store data from a user, what are the necessary tools or jobs we need, i will list it out.
- we should be able to validate the users input,
- if passed, we can store else return a message to the user.
For the following, we need the following
- A validator
- validateTicketInputJob
- TicketRepository to interface with the model
- StoreTicketJob.
- SendBackWithErrorMessageJob to give the user a message if validation fails
So lets build the following
.
lets create the validator.php file
Create a folder called validators and and the file called TicketValidator.php
paste the code below
<?php
namespace App\Domains\Validators;
use App\Foundation\AppValidator;
class TicketValidator extends AppValidator {
protected $rules = [
'title' => 'required',
'category' => 'required',
'priority' => 'required',
'message' => 'required'
];
protected $messages = [
'required' => "We'd love to know your :attribute, so please fill it properly.",
];
}
The above simply tells the user what must be filled before he or she can proceed with the ticket.
Next up is the ticket repository
Create a folder called Repositories under the data folder and create the file called ticket repository and add the code below.
<?php
namespace App\Data\Repositories;
use Framework\Ticket;
/**
* Base Repository.
*/
class TicketRepository extends Repository
{
/**
* The model instance.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
public function __construct()
{
$this->model = new Ticket;
parent::__construct($this->model);
}
}
Okay we done, let now make our jobs that we are to use in our feature.
okay i remember, we are to create the following jobs
- StoreTicketJob
- ValidateTicketInputJob
- SendBackWithErrorMessageJob
Remember all we have to do is generate them.
In yourlucid CLI enter each of the command
lucid make:job StoreTicket Ticket
lucid make:job ValidateTicketInput Ticket
lucid make:job SendBackWithErrorMessage Ticket
Find each of the jobs in the Ticket domain and paste in each file
StoreTicket.php
<?php
namespace App\Domains\Tickets\Jobs;
use Lucid\Foundation\Job;
use App\Data\Repositories\TicketRepository;
class StoreTicketJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(TicketRepository $repo)
{
$repo->model->create($this->data);
}
}
ValidateTicketInputJob
<?php
namespace App\Domains\Tickets\Jobs;
use Lucid\Foundation\Job;
use App\Domains\Validators\TicketValidator;
class ValidateTicketInputJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(TicketValidator $validator)
{
return $validator->validate($this->data);
}
}
SendBackWithErrorMessageJob.php
Note: This job should be generated in the Http domain
<?php
namespace App\Domains\Http\Jobs;
use Lucid\Foundation\Job;
use Redirect;
use Illuminate\Http\Request;
class SendBackWithErrorMessageJob extends Job
{
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Request $request)
{
$request->session()->flash('messages', $this->data['messages']);
if ($this->data['input'])
{
return Redirect::back()->withInput();
} else {
return Redirect::back();
}
}
}
We are almost done, back to our StoreTicketFeature.php
paste this
<?php
namespace App\Domains\Http\Jobs;
use Lucid\Foundation\Job;
use Redirect;
use Illuminate\Http\Request;
class SendBackWithErrorMessageJob extends Job
{
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Request $request)
{
$request->session()->flash('messages', $this->data['messages']);
if ($this->data['input'])
{
return Redirect::back()->withInput();
} else {
return Redirect::back();
}
}
}
We are done for today, just completely stored a ticket you can now check our database.
Previous Tutorial
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @sirfreeman I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x