I'm curious is that possible to pass (as type-hinted) of custom class that extends FormRequest to be passed in to action within GET request ?
for example:
at routes/api.php
Route::get('/schema', '\App\Http\Controllers\TestController#getSchema');
then I have App\Http\Requests\SchemaRequest.php
and at controller, I want get this request from route within GET method.
class TestController extends Controller {
public function getSchema(\App\Http\Requests\SchemaRequest $request) {
// do other stuff here
}
}
I've tried to look deeper and doing some hack but nothing success yet?
Is that possible?
Any input would be appreciated, and thanks for reading
How about this workaround?
Route::get('/schema', 'TestController#getSchema',namespace =>'App\Http\Controller');
class TestController extends Controller {
public function getSchema(Request $request) {
// do other stuff here
}
}
Related
In my routes file
$this->post('/form', [FormFacade::class, 'createMethod]);
In my FormFacade class:
............
...........................
public function createMethod(Request $request)
{
// do some stuff
// want to call controller's store method from here
}
In my FormController class
......................
public function store(FormRequest $request)
{
//am expecting validated data here onwards .....
//code to store validated data in the database
}
So, I need to call FormController:: create() method from my FormFacade. For that .... I need to inject the FormRequest object in the store method. How can i do that ? Any ideas is much appreciated.
You can create Requests classes.
https://laravel.com/docs/9.x/validation#creating-form-requests
Those classes extend Request class & Request class extends FormRequest so it's the same thing & you will follow Laravel Standarts
To maintain my Laravel application and save myself from a lot of duplicate code I have made the following solution:
BaseController
class BaseController extends Controller
{
public function get($id){
return $this->baseService->get($id);
}
public function getAll(){
return $this->baseService->getAll();
}
}
BaseService
class BaseService
{
protected $model;
public function __construct($model){
$this->model = $model;
}
public function get($id){
return response()->json($this->model->where('id', $id)->first());
}
public function getAll()
{
return $this->model->get();
}
}
MyController
class MyController extends BaseController
{
protected $model;
protected $baseService;
public function __construct(){
$this->model= new Model();
$this->baseService = new BaseService($this->model);
}
/**
* This controller has all the functionality from BaseController now
*/
}
What I'm wondering if this is a good method. Should I stick with this or should I use a different approach? I've heard about Traits but not sure if they are doing the same thing. It's Laravel 5.5 I'm using.
Yes, traits are used to move methods out of a controller regularly. A good example that the Laravel framework uses is the ThrottlesLogin trait. Take a look at https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Auth/ThrottlesLogins.php#L20
to see how the methods are moved outside of a controller but can be still accessed by importing the trait using the use keyword.
While traits would work for your use case I wouldn't use them here for the functionality you are looking for. I would use the repository pattern. It would better separate your code and make it more reusable.
Take a look at https://bosnadev.com/2015/03/07/using-repository-pattern-in-laravel-5/ for more information on the repository pattern. Basically, you would separate your code into a separate repository and use Laravel's built in IoC to inject the repository into your controller.
MyController
class MyController extends Controller
{
protected $repo;
public function __construct(MyRepository $myRepository)
{
$this->repo = $myRepository;
}
public function index()
{
$myStuff = $this->repo->all();
}
// you can also inject the repository directly in the controller
// actions.
// look at https://laravel.com/docs/5.5/controllers#dependency-injection-and-controllers
public function other(MyRepository $repo)
{
$myStuff = $repo->all();
}
}
This is the perfect use case for a Trait. Traits are intended for reusable functions. They're super simple to implement, and won't take more than a few minutes to change what you have.
Here is a great article on them: https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5
I currently have to declare my Stripe api key in every controller method that will make an api call to Stripe. For example
public function __construct()
{
\Stripe\Stripe::setApiKey(env('STRIPE_KEY'));
}
public function addCard()
{
Stripe::setApiKey(env('STRIPE_KEY'));
}
public function updateCard()
{
Stripe::setApiKey(env('STRIPE_KEY'));
}
public function deleteCard()
{
Stripe::setApiKey(env('STRIPE_KEY'));
}
This is getting very annoying as I have more than 10 methods that are doing this. My question is, is there a way i can declare the key somewhere ONCE and not have to call it in every controller method?
Yep, I'd do it in your AppServiceProvider. You'll find this at app/Providers/AppServiceProvider.php.
Stick it in the register method:
public function register()
{
Stripe::setApiKey(env('STRIPE_KEY'));
}
That will run during bootstrap for every request.
Alternatively you could set this in the constructor for your Controller. This would cause it to run for all actions in this one controller only.
class PaymentController extends Controller {
public function __construct() {
Stripe::setApiKey(env('STRIPE_KEY'));
}
public function addCard() {
...
I'd argue this is less desirable, your service providers should primarily be responsible for wiring up your dependencies. Up to you.
I have a function within an AdminController for sending emails. I want to access this within another controller. Can anyone advise how I would modify this to work?
OrdersController
public function postOrder()
{
$order = New Order;
...
$order->save();
// email order (call function in other controller)
$this->emailOrder($order);
}
AdminController
public function emailOrder($order)
{
//email processing goes here
}
You would strip it out, either into an abstract controller, that your controllers inherit from:
class AdminController extends MyController
Or to a service that you can call from your controller:
Mail::sendOrder($order)
How to call a model in laravel.
My code is:
use Jacopo\Authentication\Models\Guide;
class SampleController extends BaseController
{
public function index()
{
$model='Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
This will produce Class 'Guide' not found error.
If you added your class you should run in terminal
composer dump-autoload
to update your class map. Otherwise autoloader may not "see" your class and you are getting this error.
You need to add the namespace to your string:
class SampleController extends BaseController
{
public function index()
{
$model='Jacopo\Authentication\Models\Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
You could also resolve it from the IoC container, but you need to register it first:
App::bind('Guide', 'Jacopo\Authentication\Models\Guide');
And then you should be able to:
$model = App::make('Guide');
$guide = $model::where('guide_link','=',"guide")->get();
But this is not a very good option