App\Http\Controllers\PostController` is not invokable: - php

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']);

Related

Auto create logs (inserts data to table) whenever a function was called

I'm new to laravel and to php oop. My main goal is to call createLogs() everytime a function is called without putting the call method in each function because it's a hassle. I need help please.
I made a controller called WebLogs with a function called createLogs() that inserts data to a table. I want it to be auto-called whenever another function is called. I tried using this solution and put it in Controller class because WebLogs extends Controller class, and all my other controllers extends Controller class, but the solution doesn't seem to work.
So my Controller class now looks like this:
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
// Added this function from the solution I mentioned
public function __call($method, $arguments) {
echo 'hello world';
echo '<br><br>';
echo $method;
if(method_exists($this, $method)) {
return call_user_func_array(array($this,$method),$arguments);
}
}
}
Example controller:
class DashboardController extends Controller
{
public function index()
{
(new WebLogs)->createLogs(); //I don't want to call this for every function
return view('dashboard');
}
public function showSomething()
{
(new WebLogs)->createLogs();
return view('something');
}
public function updateSomething()
{
(new WebLogs)->createLogs();
return redirect()->back()->with('message','yeey');
}
}
How the functions from DashboardController are being called from web.php:
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/something', [DashboardController::class, 'showSomething'])->name('something');
Route::post('/something/update', [DashboardController::class, 'updateSomething'])->name('something.update');
Create app\Classes\WebLogs.php with content:
<?php
namespace App\Classes;
class WebLogs {
public function __construct() {
return "WebLogs class with construct function was initialized.";
}
public function createLogs($routeName,$routePath) {
$status = 0;
logger('WebLogs class is running:');
logger([$routeName,$routePath]);
// Save to database here
// ...
return $status;
}
}
Then, create an AutoCreateLogs middleware, it will save as app\Http\Middleware\AutoCreateLogs.php:
$ php artisan make:middleware AutoCreateLogs
With content:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Classes\WebLogs;
class AutoCreateLogs
{
public function handle(Request $request, Closure $next)
{
$route = Route::current();
$routePath = $route->uri;
$routeName = $route->action['as'];
$w = new WebLogs;
$w->createLogs($routeName,$routePath);
return $next($request);
}
}
And use this middleware like this:
Route::middleware([AutoCreateLogs::class])->group(function () {
Route::get('/', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');
Route::get('/something', [App\Http\Controllers\DashboardController::class, 'showSomething'])->name('something');
Route::post('/something/update', [App\Http\Controllers\DashboardController::class, 'updateSomething'])->name('something.update');
Route::get('/something/{value}', [App\Http\Controllers\DashboardController::class, 'getSomething'])->name('get.something');
});
With app\Http\Controllers\DashboardController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
$page = 'index';
return view('welcome', ['page'=>$page]);
}
public function showSomething()
{
$page = 'showSomething';
return view('welcome', ['page'=>$page]);
}
public function updateSomething()
{
$page = 'updateSomething';
return response()->json(['page'=>$page]);
}
public function getSomething(Request $request)
{
$page = 'getSomething';
return view('welcome', ['page'=>$page]);
}
}
Then, empty storage\logs\laravel.log, and run with example route
http://laravel-me.com/something/value99
It will show the successful result:
[2022-02-18 22:56:09] local.DEBUG: WebLogs class is running:
[2022-02-18 22:56:09] local.DEBUG: array (
0 => 'get.something',
1 => 'something/{value}',
)
Read more about middleware: https://laravel.com/docs/8.x/middleware

Laravel of Controller Function Should be Compatible with Base Controller

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) {
...
}
}

Laravel 5.7: Class 'App\Http\Controllers\MailableClass' not found

I created a Mailable called Class UserRequest
I'm trying to call it from inside a controller buy this is the error I get:
Class 'App\Http\Controllers\UserRequest' not found
I also tried ->send(new \UserRequest($msgdata)); but it still doesn't work.
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request)
{
$msgdata = array('subject'=>$request->subject,'email'=>$request->email, 'name'=>$request->name,'body'=>$request->body);
try
{
Mail::to('dddddddd#dddsdsf.com')
->send(new UserRequest($msgdata));
}
catch(Exception $e)
{
}
}
}
Include your class at the top like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\UserRequest; // including your class
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request){
$msgdata = array('subject'=>$request->subject,'email'=>$request->email,
'name'=>$request->name,'body'=>$request->body);
try {
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
}catch(Exception $e){
// Log your exception
}
}
}
add 'App\Http\Controllers\UserRequest' to the head
use App\Http\Controllers\UserRequest;
at the top.
You will need to add the right path to the top as stated by other.
Also check the namespace in the UserRequest class

Class App\Http\Controllers\Panel does not exist

I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}

Laravel return variable in blade template

I have a route like below.
Route::get('profile/{username}', 'ProfileController#index');
ProfileController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
function index() {
view('profile.index');
}
}
profile/index.blade.php
{username}
But it doesn't echo the username when I go to /profile/salep, what's missing here?
If I change my ProfileController to this (below), it works but PhpStorm says "Unreachable stamement" for the view.
class ProfileController extends Controller
{
function index($username) {
return $username;
view('profile.index');
}
}
I didn't use the structure below (took it from the documentation) because I need to pass my variable to a view rather than returning, so I need to both return and pass it to a view, I guess.
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
You were nearly there with the second attempt.
Try this:
class ProfileController extends Controller
{
function index($username) {
return view('profile.index')->with('username', $username);
}
}
I solved it.
routes.php
Route::get('profile/{username}', 'ProfileController#index');
ProfileController.php
class ProfileController extends Controller
{
function index($username) {
return view('profile.index')->with('username', $username);
}
}

Categories