Target [App\Http\Controllers\Traits\FileUploadTrait] is not instantiable.
I get this error when trying to send a file upload to this route:
<?php
namespace App\Http\Controllers\Traits;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
trait FileUploadTrait
{
/**
* File upload trait used in controllers to upload files
*/
public function saveFiles(Request $request)
{
//some file upload code
}
}
in my route:
Route::post('upload/files', ['uses' => 'Traits\FileUploadTrait#saveFiles', 'as' => 'media.upload']);
how to use a trait as a route controller#method?
You can't use a Trait as controller since Trait's are not classes, but actually they "are a mechanism for code reuse in single inheritance languages such as PHP." (See php docs).
A trait is not instantiable, this means that under the hood, Laravel can't do
$controller = new FileUploadTrait() to use it.
To use a trait, you must include it on some class, for example:
class MyController {
use FileUploadTrait;
}
Then you define your routes to use that class that you define.
traits arent callable... which means :) you cannot call them :Dlol, sorry - couldnt help myself... anyway try with something like this :)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
class MahController extends Controller {
use App\Http\Controllers\Traits\FileUploadTrait;
}
Traits cannot be 'instantiated', you add them on objects.
Related
I'm currently working on a PHP trait thay will help me to reuse code in some class controllers that I have using Laravel framework.
I wanted to make the trait methods as dynamic as I could but when trying to access to a class that my parent class imported, I get a Class not found exception.
My class controller is as follows:
namespace App\Http\Controllers\Admin;
use App\Models\ {
Curso,
Leccion,
Diapositiva,
ImagenDiapositiva
};
use App\Traits\TestTrait;
class DiapositivasController extends Controller{
use TestTrait;
public function addRecord(Request $request){
$request->class_name = 'ImagenDiapositiva';
$this->addImage($request);
}
}
My Trait:
namespace App\Traits;
trait TestTrait{
public function addImage($request){
$class_name = $request->class_name;
$diapositiva = new $class_name;
//extra code
}
}
So my doubt is, do I have to include the model classes I want to use inside my Trait again or am I doing something else wrong?
if you use new with a variable class name, you have to use the fully qualified class name. I'm guessing new $class_name is the root cause of the issue here, since $class_name would have to be something like: 'App\Models\ImagenDiapositiva' or whatever the full namespace is. Just have to change the call $request->class_name = 'ImagenDiapositiva'; to reflect the full name of the class.
I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}
I have followed some tutorials to create some global helper functions to use in blade views.
I have created ViewHelpers.php file in App\Helpers folder. This file contains the following code:
<?php
class ViewHelpers {
public static function bah()
{
echo 'blah';
}
}
Here is my service provider which loads my helpers (currently just one file):
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider {
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
echo $filename; // for debugging - yes, I see it is getting called
require_once($filename);
}
}
}
I have added it to config\app.php in 'providers' section:
'App\Providers\HelperServiceProvider',
And now I call my helper in a blade view:
{{ViewHelpers::bah()}}
For now it works fine.
But if I change my ViewHelper namespace to this:
<?php namespace App\Helpers;
class ViewHelpers {
// omitted for brevity
my views fail with Class 'ViewHelpers' not found.
How do I make my views to see the ViewHelpers class even if it is in a different namespace? Where do I add use App\Helpers?
Another related question - can I make an alias for ViewHelpers class to make it look like, let's say, VH:bah() in my views?
And I'd prefer to do it in simple way, if possible (without Facades and what not) because these are just static helpers without any need for class instance and IoC.
I'm using Laravel 5.
You will get Class 'ViewHelpers' not found because there is no ViewHelpers, there is App\Helpers\ViewHelpers and you need to specify namespace (even in view).
You can register alias in config/app.php which will allow you to use VH::something():
'aliases' => [
// in the end just add:
'VH' => 'App\Helpers\ViewHelpers'
],
If your namespace is correct you do not even have to use providers - class will be loaded by Laravel.
I have created a common class in app/Classes/Common.php
but whenever i try to access a model in a class function.
$new_booking_request = BookingRequest::where('host_id','=',Auth::id())
I am getting this error
Class 'App\Models\BookingRequest' not found
Even other classes like Auth, URL and Cookie are not working.
Is there a way to bring all classes in my Common class scope?
You get this issue when your namespace is wrong you or you forgot to namespace.
Since common.php is inside App/Classes, inside Common.php do somethng like this:
<?php namespace App\Classes;
use View, Auth, URL;
class Common {
//class methods
}
Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\Models then inside BookingRequest.php do this:
<?php namespace App\Models;
BookingRequest extends \Eloquent {
//other definitions
}
Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so:
<?php namespace App\Classes;
use App\Models\BookingRequest;
use View, Auth, URL;
class Common {
//class methods
}
In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified.
To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. Also you can call the class globally with the \. Ex: \Auth, \URL and \Cookie
For the namespace in L5 here is a quick example:
<?php namespace App\Models;
class BookingRequest {
// class definition
}
then when trying to call that class, either call the full namespace path of the function, or include the function.
<?php
class HomeController extends Controller {
public function index()
{
$newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
}
}
OR
<?php namespace App\Controllers;
use App\Models\BookingRequest; // Include the class
class HomeController extends Controller {
public function index()
{
$newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
}
}
PS:
Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework.
I have a problem. I am building an administration panel for my application and decided, because of the many functions, to use a RESTfull route. Now, because I do not want to jam every function in the same class I also use namespacing and extend my AdminController class.
The problem is, RESTFull works for the functions declared in the AdminController file, but it does not recognize the functions deeper within the namespace. What is the correct way to do this?
This is the code I have right now:
RESTfull Route
Route::controller('admin', 'Admin\AdminController');
AdminController (/controllers/AdminController.php)
namespace Admin;
use View;
class AdminController extends \BaseController {
public function getSales() {
echo"Works";
}
DashboardController (/controllers/admin/DashboardController.php
namespace Admin;
use AdminController;
use View;
class DashboardController extends AdminController {
public function getDashboard() {
echo"Does not work";
}
I can access www.domain.com/admin/sales just fine, but when I access www.domain.com/admin/dashboard it gives me a "Controller method not found" error.
I think you should provide this route manually:
Route::controller('admin/dashboard', 'Admin\DashboardController');
In your code Laravel has no idea that it should use DashboardController and not AdminController for admin/dashboard route