I have to make validation of all of Date format only using class/request in Laravel. Can I make validation for all of the requests ? I think i do it in request.php abstract class.
You may try something like this, at first create a BaseController like the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BaseController extends Controller {
public function __construct(Request $request) {
$this->request = $request;
$this->hasValidDate();
}
protected function hasValidDate()
{
if($this->request->method() == 'POST') {
// Adjust the rules as needed
$this->validate($this->request, ['date' => 'required|date']);
}
}
}
Then in your other controllers, extend the BaseController like this example:
namespace App\Http\Controllers\User;
use App\Http\Controllers\BaseController;
class UserController extends BaseController {
public function index()
{
// ...
}
}
Hope you got the idea. Use it wisely.
Related
Greetings guys i'm having a challenge figuring out how to make this method in the base controller so that i initialize it there and call it in all other controllers that i wish.
I want to create this in the base controller , then call it in other controllers
$paynow = new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
Images
1.This is what i have done in the base controller
Image 2. This is where im trying to use it to call its member functions
Image 3. Is the error that im getting
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected function callPayNow()
{
return new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
}
}
In your AnyController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AnyController extends Controller
{
public function anyMethod()
{
$this->callPayNow();
}
}
if I undersend you well,
Maybe the best approach is to create new controller that extends BasController:
class Controller extends BaseController
{
public function payNow()
{
return new Paynow(
'9644',
'7e3bebb4-6dbf-4f8f-9e10-aceafd02c8db',
'Return_url',
'Result_url'
);
}
}
And then in your other controller you can extend your new controller:
class UserController extends Controller
{
//For example
public function show($id, Request $request)
{
$payNow = $this->payNow();
$payment = $payNow->createPayment($currentOrder, $request->get('email'));
return response()->json("done");
}
}
Paynow will be called in every controller that extends this controller.
I have few classes which extends from the abstract class
And Class MenuController Extends from SiteAdminController
I need to call MenuController and receive authenticated user id
<?php
namespace App\Http\Controllers\SiteAdmin;
use App\Http\Categories;
use Illuminate\Http\Request;
use Gate;
use App\Category;
use App\Http\Controllers\MenuController;
use App\Site_categories;
use Auth;
class SiteAdminController extends \App\Http\SiteEntity implements Categories
{
protected $host;
public $user;
public function __construct()
{
parent::__construct();
$this->middleware('auth:admin');
}
protected function menu() {
return $data_nav['menu'] = MenuController::index('admin_categories');
}
Other one extends from SiteAdminCntroller
<?php
namespace App\Http\Controllers\SiteAdmin;
use Illuminate\Http\Request;
use Gate;
use Auth;
use App\Category;
class MenuController extends SiteAdminController
{
public $category_menu;
public $user_categories;
public $user;
public function __construct(Auth $auth)
{
//parent::__construct();
$this->user_categories=$this->CategoriesMenu();
$this->user=$auth::guard('admin')->user()->id;
dd($this->user);
//dd($this->user_categories);
}
I think the constructor in the MenuController run befor the middlware in SiteAdminController
Thats why I have such error
http://prntscr.com/hwfifx
Please Explaine what have I do to see result from me dd() function?
I was trying even to call parent::__construct but it not helping
You are correct that the the code in the constructor runs before the middleware: https://github.com/laravel/framework/issues/15072
The easiest way to get around this is to use the middleware method in the controller:
MenuController
public function __construct()
{
parent::__construct();
$this->middleware(function () {
$this->user_categories = $this->CategoriesMenu();
$this->user = auth()->guard('admin')->user()->id;
});
}
First of all check if the class see another class that must be extended with.
Then try below approach (it s just example):
class ConceptController extends \SiteAdminController {
public function __construct(SiteAdminController $siteAdmin) {
parent::__construct($siteAdmin);
}
}
I want to call a function of CommonController in PostsController.
PostsController is in Posting namespace
PostController: App\Http\Controllers\Posting\PostsController
CommonController: App\Http\Controllers\CommonController
I tried this code but it did not work in PostingController, it is a small code from my PostingController
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\CommonController;
class PostsController extends Controller
{
public function myFunction(Request $request, $id){
$commonControllerObj = new CommonContoller;
$result = $commonControllerObj->commonCallingFunction($id);
}
}
but it did not work, its giving error
Class 'App\Http\Controllers\Posting\CommonContoller' not found
The first your namespace is wrong
namespace App\Http\Controllers\Posting;
The second you can call another Controller like this
app('App\Http\Controllers\CommonController')->commonCallingFunction();
This will work, but this is bad in terms of code organisation
You can extends Controller like this
use App\Http\Controllers\CommonContoller;
class PostsController extends CommonContoller
{
public function myFunction(Request $request, $id){
$result = $this->commonCallingFunction($id);
}
}
This is my code
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
use ResetsPasswords;
public function postReset(Request $request){
// do some stuff
// ...
return parent::postReset($request); // <-here is the problem
}
The method postReset is present in ResetsPasswords, but the code I've written is looking for this method within the Controller class.
Any ideas?
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller {
use ResetsPasswords {
ResetsPassword::postReset as traitPostReset;
};
public function postReset(Request $request){
// do some stuff
// ...
return $this->traitPostReset($request);
}
}
The reason why parent::postReset($request) issues a fatal error is because its parent Controller class hasn't postReset method. The trait isn't considered as a parent in that case even if it has an implementation of postReset() method.
To work-around this, you can give it a different name:
class PasswordController extends Controller
{
use ResetsPasswords;
public function postResetPassword(Request $request) // <- giving it a different name
{
// do some stuff
// ...
return $this->postReset($request); // and calling ResetsPasswords::postReset
}
I have a problem with passing variable (data) to all Views. I created BaseController that extends default Laravel controller and "global" variables are defined there. When I extend other controller with BaseController i got error that variable is not defined. Does someone knows where's the problem?
Here is code:
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class BaseController extends Controller {
public function __construct() {
$obavijesti="Other data";
$izbornici="Some data";
View::share ( 'izbornici', $izbornici );
View::share ( 'obavjesti', $obavjesti );
}
}
class AdminController extends BaseController {
.
.
.
echo '<pre>';var_dump($izbornici);echo '</pre>';//Error pop ups here
.
.
.
}
You are doing something wrong here. view::share() is used for sharing a piece of data across all views not controller.
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
//If you wish to get these variables in your other controllers you do this:
class BaseController extends Controller {
public $obavijesti="Other data";
public $izbornici="Some data";
public function __construct() {
View::share ( 'izbornici', $this->izbornici );
View::share ( 'obavjesti', $this->obavjesti );
}
}
class AdminController extends BaseController {
//if you have a constructor in other controllers you need call constructor of parent controller (i.e. BaseController) like so:
public function __construct(){
parent::__construct();
}
public function Index(){
echo $this->obavijesti;
}
}
You can also use a composer to share variables to views
//1. Create a composer file at app\Composers\AdminComposer.php
//NB: create "app\Composers" if does not exists
//2. Inside AdminComposer.php add this.
<?php namespace App\Composers;
class AdminComposer
{
public function __construct()
{
}
public function compose($view)
{
//Add your variables
$view->with('izbornici', 'Other data')
->with('obavjesti', 'Some other data');
}
}
//3. In you controller do this:
<?php namespace App\Http\Controllers;
//NB: I removed your BaseController because I believe the issue is coming from //there
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Sentry;
use Illuminate\Http\Request;
use View;
class AdminController extends Controller{
public function __construct(){
//Lets use AdminComposer to share variables to adminpage.blade.php view
View::composers([
'App\Composers\AdminComposer' => array('adminpage')
]);
}
public function Index(){
return view('adminpage');
}
}
Ideally you're going to want a combination of what you have, and the other answer posted here.
<?php
class BaseController extends Controller {
protected $obavijesti = 'Other data';
protected $izbornici = 'Some data';
public function __construct() {
View::share('obavjesti', $this->obavjesti);
View::share('izbornici', $this->izbornici);
}
}
Then in all of your views, you have access to the variables $obavjesti and $izbornici. Now in your other controllers, anything that extends BaseController can do the following:
class AdminController extends BaseController {
public function index() {
echo $this->ixbornici;
echo $this->obavjesti;
}
}