I created a new route link to Users Controller validate function in web.php. I also create a function on Users Controller called validate. Other routes has no problem other than this.
Error
(1/1) ErrorException
Declaration of App\Http\Controllers\UsersController::validate(App\$id) should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
Web routes
Route::group(['middleware' => 'can:see-admin'], function () {
Route::resource('users', 'UsersController', ['only' => ['index', 'destroy', 'create', 'validate']]);
Route::post('users/store', 'UsersController#store')->name('user.store');
Route::get('users/{user}/impersonate', 'UsersController#impersonate')->name('users.impersonate');
Route::get('users/{id}', 'UsersController#validate')->name('users.validate'); //this is the new route that is created
});
Users controller
class UsersController extends Controller
{
public function validate($id)
{
$validation = User::validate($image);
return back();
}
}
Controller
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
1: Don't use this Method name, it is be used by validator.
2: Try to use another action nameBecause you have missing the Request $request in your action, try to change it like this:
use Illuminate\Http\Request; // Remember import Request
...
class UsersController extends Controller
{
public function validateUser(Request $request, $id) {
...
}
}
Related
UnexpectedValueException
Invalid route action: [App\Http\Controllers\PostController].
at
C:\Users\DELL\Desktop\xampp\htdocs\Anka\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:92
88▕ */
89▕ protected static function makeInvokable($action)
90▕ {
91▕ if (! method_exists($action, '__invoke')) { ➜ 92▕ throw new UnexpectedValueException("Invalid route action:
[{$action}].");
93▕ }
94▕
95▕ return $action.'#__invoke';
96▕ }
i App\Http\Controllers\PostController is not invokable: The
controller class App\Http\Controllers\PostController is not
invokable. Did you forget to add the __invoke method or is the
controller's meth od missing in your routes file?
1
C:\Users\DELL\Desktop\xampp\htdocs\Anka\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:47
Illuminate\Routing\RouteAction::makeInvokable()
2
C:\Users\DELL\Desktop\xampp\htdocs\Anka\vendor\laravel\framework\src\Illuminate\Routing\Route.php:190
Illuminate\Routing\RouteAction::parse()
PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return view('posts/index');
}
}
Posts endpoints
Route::get('/posts', PostController::class, 'index');
Try this
PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return view('posts/index');
}
}
//posts endpoints
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
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 can not find the error:
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller{
public function index(){
return 'Home page do site';
}
}
My Route:
You have not defined the route. we define route "web.php" of our laravel file.
Route::get('/index', 'SiteController#index')->name('home');
this migght work for you.
My problem:
I have a controller that calls a method from another controller for some information. However, Laravel isn't able to locate the class in that controller.
FatalErrorException in TradesController.php line 35: Class 'Profile'
not found
What I have tried:
I am using Laravel 5.2 and have created the controller with php artisan make:controller Profile to ensure that any possible internal pointers (in lack of better vocabulary) are created - even though my understanding is that Laravel 5.2 does this automatically as long as controllers are in the \app directory.
They both reside within \app\Http\Controllers
My code
TradesController calls class Profile
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Profile;
use Session;
use Auth;
class TradesController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function showInventory (Request $request){
.....
// Following three calls all fail
$profile = new Profile;
$profile = Profile()->linkToProfile();
$profile = Profile::linkToProfile();
.....
return($output);
}
}
MY class Profile-controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use Auth;
class ProfileController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function linkToProfile (Request $request) {
return("test");
}
}
php artisan make:controller xxx creates a class xxxController therefore your class is ProfileController not Profile.
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.