I am re-doing a part of a website and decided to use Laravel 8 with its built in Repository Pattern. I set up the project and firstly did everything in a WelcomeController that handles all front page methods. Everything worked great.
I then abstracted the parts of the WelcomeController methods' functionality
1> Validation of forms => App\Http\Requests\WelcomeRequest
2> Methods used => App\Interfaces\WelcomeInterface
3> Methods used implemented => App\Repositories\WelcomeRepository
4> Injected interface => App\Http\Controllers\WelcomeController
5> Binded Interface + Repository => Providers\RepositoryWebServiceProvider
6> Registered service provider RepositoryWebServiceProvider
=> config/app.php
7> Commands => php artisan config:cache
& composer dump-autoload
The front page loaded. Hurrah!
I then posted contact form values with ajax and received a 403 Forbidden
http response code.
A] To try and fix this I replaced the WelcomeRequest
argument from the WelcomeController store
method with instance of Illuminate\Http\Request
and the request was deposited into the WelcomeController store
method.
B] But again errorred out on injected interface method $this->welcomeInterface->contactEmail($request)
with error message expected WelcomeRequest $request instance Illuminate\Http\Request given
.
C] The 403 forbidden error only occurs when intstantiating the App\Http\Requests\WelcomeRequest
class.
App\Http\Controllers\WelcomeController
use App\Http\Requests\WelcomeRequest;
use App\Interfaces\web\WelcomeInterface;
class WelcomeController extends Controller
{
protected $welcomeInterface;
public function __construct(WelcomeInterface $welcomeInterface)
{
$this->welcomeInterface = $welcomeInterface;
}
// ...
public function store(WelcomeRequest $request) // the request argument causes the error
{
return $this->welcomeInterface->requestContact($request);
}
Routes\web.php
Route::post('/email-contact', [App\Http\Controllers\WelcomeController::class, 'store'])->name('email-contact');
App\Http\Requests\WelcomeRequest
class WelcomeRequest extends FormRequest
{
public function authorize()
{
return false;
}
public function rules()
{
return [
'name'=> 'required|string|max:60',
'email'=> 'required|string|email|max:255',
'subject' => 'required|string|max:60',
'message' => 'nullable|string|max:300',
];
}
public function messages()
{
return [
'name.required' => 'Your name is required and should not exceed maximum length of 60.',
'name.max' => 'Your name is required and should not exceed maximum length of 60.',
'email.required' => 'Email is required.',
'email.email' => 'Email is incorrect format.',
'subject.required' => 'The subject is required and should not exceed maximum length of 60.',
'subject.max' => 'The subject is required and should not exceed maximum length of 60.',
'message.max' => 'Comment maximum length exceeded.',
'message.string' => 'Comment is invalid format.',
];
}
}
My question is why is the use of the App\Http\Requests\WelcomeRequest
class that extends Illuminate\Foundation\Http\FormRequest
giving me a 403 Forbidden
http response? How do I solve this?
Solved it. The WelcomeRequest
class needed to be authorised before allowing validation. Therefore I changed the following method return --
public function authorize()
{
return true;
}