I am getting the following error:
The autoloader expected class "Acme\HelloBundle\Controller\HelloController" to be defined in file "/var/www/Symfony/app/../src/Acme/HelloBundle/Controller/HelloController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
The controller code I have is actually:
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
any idea why this is?
<?php namespace Acme\HelloBundle\Controller;
....
Just add the "*LESS_THAN*"?php tag at the beginning. Try if works.
Your controller should extend Symfony\Bundle\FrameworkBundle\Controller\Controller
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use namespace Acme\HelloBundle\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
Related
I'm trying to use a trait to handle image upload on my Laravel application, but none of the functions in my Trait can be called from the controller.
It throws a BadMethodCallException and says that the function couldn't be found.
I've tried using really simple functions to test if it is a problem with the trait or whether the function itself has an issue, but even a simple return function that only contains
return "sampletext";
has the same issue.
The path of the trait is under App/Traits/UploadTrait
and I've already checked the spelling on the use statement in my controller, which says use App\Traits\UploadTrait;
namespace App\Traits;
trait UploadTrait
{
public function test(){
return "testtext";
}
}
And the controller has
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use App\User;
use App\Profile;
use App\Traits\UploadTrait;
use Image;
class UserProfileController extends Controller
{
...
protection function updateProfile($args, Request $request){
...
return $this->test();
...
Of course I expect the function in my trait to be called, but this does not happen.
You need to use the trait inside your controller and move the $this->test() inside a class function:
<?php
use App\Traits\UploadTrait;
class UserProfileController extends Controller
{
use UploadTrait; // <-- Added this here
public function index()
{
return $this->test(); // <-- Moved this into a function
}
}
You have to put the use keyword to use that trait and its methods in the class
trait UploadTrait
{
public function test(){
return "testtext";
}
}
class Controller{
}
class UserProfileController extends Controller
{
use UploadTrait;
}
$ob = new UserProfileController();
echo $ob->test();
You can make a function to and call the trait function.
More Details
Use trait inside the class like:
use my/path/abcTrait;
Class My class{
use abcTrait;
}
Now, you can call trait functions with $this->functionName () in functions.
I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.
I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
I have two classes in the services layer
CashTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashTransaction;
use App\Models\Cash\CashBoxTransaction;
use DB;
class CashTransactionServices {
protected $AccountingAPI;
public function __construct(AccountingAPI $acc) {
$this->AccountingAPI = $acc;
}
CashBoxTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashBoxTransaction;
class CashBoxTransactionServices extends CashTransactionServices {}
and in the controller
namespace App\Http\Controllers\Cash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Cash\CashBoxTransactionServices;
class CashBoxTransactionController extends Controller {
//
private $services;
public function __construct(CashBoxTransactionServices $cash_box_transaction_services) {
$this->services = $cash_box_transaction_services;
}
I have this error
ReflectionException: Class App\Services\Cash\CashBoxTransactionServices does not exist in file C:\xampp\htdocs\easyerp\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 826
I made sure of using the right namespaces and the right classes names!
PS: when I remove the inheretance from CashBoxTransactionServices class It works!
I am new to Laravel and checking out some sample code.
In a controller I see this:
<?php
use Illuminate\Support\Facades\Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
Why do I have to use the "use Illuminate\Support\Facades\Input;" ?
Cant I just use eg Input::get(); like I do in my route file?
<?php
use Illuminate\Support\Facades\Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
this controller is in global namespace. so you don't need to use use Illuminate\Support\Facades\Input; you can directly call Input::get('foo');
<?php namespace Foo; //<---- check the namespace
use Input;
class RegistrationController extends \BaseController {
public function __construct()
{
$this->beforeFilter('guest');
}
here you can write either, use Input or \Input::get('foo') while calling.
You don't have to use importing namespaces (you don't need to add use Illuminate\Support\Facades\Input;) here.
You can accesss Input facade, using Input::get('something') as long as your controller is in global namespace. Otherwise you need to use \Input::get('something') or add use Input after <?php.
I'm trying to set up PSR-4 within a new Laravel 4 application, but I'm getting some troubles achieving what I want when it comes to build controllers.
Here's what I have now :
namespace MyApp\Controllers\Domain;
class DomainController extends \BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = \View::make('domain.home');
}
}
I'm not so fond of using \View, \Config, \Whatever to use Laravel's classes. So I was wondering if I could put a use Illuminate\View; to be able to use View::make without putting a \.
Unfortunately, while doing this, I'm getting the following error : Class 'Illuminate\View' not found.
Could somebody help with this please ?
The problem in your case is that View is not located in Illuminate namespace but in Illuminate\View namespace, so correct import would be not:
use Illuminate\View;
but
use Illuminate\View\View;
You can look at http://laravel.com/api/4.2/ to find out which namespace is correct for class you want to use
Assuming BaseController.php has a namespace of MyApp\Controllers\Domain
namespace MyApp\Controllers\Domain;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If BaseController.php has other namespace, i.e MyApp\Controllers
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If, for instance, you controller needs to use another base class from Laravel, lets say Config.
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
use Config;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home')->withName(Config::get('site.name'));
}
}
The use of View::make() takes advantage of the Laravel facades. To properly reference the facade, instead of directly referencing the class that gets resolved out of the iOC container, I would use the following:
use Illuminate\Support\Facades\View;
This will reference the View facade that is being used when calling View::make()