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.
Related
this is my parent class which is a user class that has the main crud operations
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\UserRepository; //<----------- Here
class UserController extends Controller
{
protected $model;
public function index()
{
$users = $this->model::all();
return view('users.index', compact('users'));
}
}
this is my child class which is one of my user roles , it have the same crud operation but it need some more functinality
<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use App\Http\Controllers\UserController;
class TeacherController extends UserController
{
public function __construct()
{
$this->model = Teacher::class;
}
}
when I try to access the route i get this error : Class name must be a valid object or a string
at :
$users = $this->model::all();
Well, it seems my Laravel project used old cached routes. Just run
php artisan route:clear
from time to time before debugging anything.
I have created the web route correctly.
use App\Http\Controllers\FrontendCourseController;
Route::get('update-item', [FrontendCourseController::class, 'update'])->name('update-item');
and add a method update. Also, I have created controller correctly.
<?php
namespace App\Http\Controllers;
use App\Helpers\CurrencyHelper;
use Auth;
use DB;
use App\Models\Course\Course;
use App\Models\Item;
class FrontendCourseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function update()
{
dd('Page Update is working');
return view('update-item');
}
}
But I keep getting this error message
Method App\Http\Controllers\FrontendCourseController::update does not
exist.
Despite clearing caches using
php artisan route:cache
Where am I doing wrong?
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 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);
}
}
I'm learning how to do a simple Laravel app, an social network and I came across an error
[Fri Aug 26 00:46:30 2016] PHP Fatal error: Class 'App\Http\Controllers\Post' not found in C:\xampp\htdocs\social-network\app\Http\Controllers\PostController.php on line 13
My PostController.php is this:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function postCreatePost(Request $request)
{
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
My Post model is this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
I don't understand what I did wrong. Can anyone help me figure out the error. Thanks in advance
You need to add the model.
Try to add following line in your controller after namespace
use App\Post;
This is what the error message exactly telling you.
You forgot to use tell your controller to use App\Post:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller{
public function postCreatePost(Request $request){
$post = new Post();
$post->body = $request['body'];
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
Also, typically good idea to make sure you run composer dump-autoload after adding new classes.