I am new to OOPS thus want to clarify things. I have this code below which works perfectly fine.
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Now I don't intend to use use keyword as it is going to be used once also wanted to experiment and thus, I used the code below.
namespace App\Http\Controllers\Admin;
class AdminController extends App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Now the above mentioned code without use keyword throws a fatal error exception. Why does that happen? In theory,I think I am doing exactly what is supposed to be done then why the exception?
You will have to import it from global namespace , below code will work fine
namespace App\Http\Controllers\Admin;
class AdminController extends \App\Http\Controllers\Controller
{
public function index()
{
echo "admin controller";
}
}
Use keyword import class from global namespace, but
class AdminController extends App\Http\Controllers\Controller
it will import parent class relative you current namespace (namespace App\Http\Controllers\Admin) , so translated path will be: App\Http\Controllers\Admin\App\Http\Controllers\Controller which is invalid.
Related
I am using laravel 5.6, when create controller and when run controller through route, I am facing error like
Declaration of App\Http\Controllers\XyzController::xyz(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::xyz($job)
My Code is
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request)
{
return view('xyz.xyz');
}
}
Missing route parameter: $job
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request, $job)
{
return view('xyz.xyz');
}
}
The base Controller that XyzController extends from defines a method named xyz with a different signature than the one you are defining.
You will need to adjust the method in XyzController to match the signature of xyz in the base Controller or adjust the base Controller to have a different signature.
Example of the problem:
class A
{
public function xyz($obj) {}
}
class B extends A
{
public function xyz(Illuminate\Http\Request $request) {}
}
Declaration of B::xyz(Illuminate/Http/Request $request) should be compatible with A::xyz($obj)
You forgot to use controller?
use App\Http\Controllers\Controller as Controller
Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData
I'm using Laravel 5.2. I tried to resolve a dependency in Laravel out of the IOCContainer as follows.(with App::make method)
App/FooController.php:-
<?php
namespace App\Http\Controllers;
use App\Bind\FooInterface;
use Illuminate\Support\Facades\App;
class FooController extends Controller
{
public function outOfContainer(){
dd(App::make('\App\bind\FooInterface')); // Focus: program dies here!!
}
}
Bindings for the FooInterface done in the AppServiceProvider as follows
App/Providers/AppServiceProvider.php:-
<?php
namespace App\Providers;
use App\Bind\Foo;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('\App\Bind\FooInterface', function() {
return new Foo();
});
}
}
Structure of the Foo class as follows.
App/Bind/Foo.php:-
<?php
namespace App\Bind;
class Foo implements FooInterface {
}
Structure of the FooInterface interface as follows:-
<?php
namespace App\Bind;
interface FooInterface {
}
Then I created a route as follows.
Route::get('/outofcontainer', 'FooController#outOfContainer');
But when I navigate to this route it throws an exception with the error text:
BindingResolutionException in Container.php line 748:
Target [App\bind\FooInterface] is not instantiable.
What is going wrong with this?
How to use App:make() with the AppServiceProvider?
In your service provider, you're binding the string '\App\Bind\FooInterface'. In your controller, you're attempting to make the string '\App\bind\FooInterface'. These strings are not the same, as they have differing cases (Bind vs bind). Since the strings are not the same, Laravel cannot find the binding in the container.
Correct the casing in your make statement and it should work:
dd(App::make('\App\Bind\FooInterface'));
I am starting to learn about Laravel 5 repository pattern, and I can't get anything right! I probably used wrong namespace or 'use'; This is the error i am getting: Class App\Http\Controllers\ServiceController does not exist
This is my Controller:
namespace App\Http\Controllers;
namespace Repo;
use App\Http\Controllers\Controller;
use Repo\ServiceRepository;
class ServiceController extends Controller {
public function __construct(ServiceRepository $service){
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Here's the repository:
class ServiceRepository {
public function getAllServices(){
return \App\Service::all();
}
}
And here's my route:
Route::get('/services', 'ServiceController#showServices');
I've tried to change some namespaces but i end up with more errors :/
You have to read a bit about namespaces in PHP.
If your ServiceController is located in app/Http/Controllers directory, then the code has to be like follows:
<?php
namespace App\Http\Controllers;
use Repo\ServiceRepository;
class ServiceController extends Controller
{
public function __construct(ServiceRepository $service)
{
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Note that I removed namespace Repo and the first use statement.
A class has to have only a single namespace, so I removed the second Repo namespace.
Namespaces are really simple. In most cases namespace of a class just reflects a path to that class. In your case, it's App\Http\Controllers because your controller is probably located in app/Http/Controllers directory.
I've removed use App\Http\Controllers\Controller because your ServiceController and App\Http\Controllers\Controller are in the same directory, so they are in the same namespace. When two classes are in the same namespace, a full path with a namespace is not necessary for them to be able to access each other.
Hope that this helps.
I've built an API in Laravel, versioned as follows:
/controllers/api/v1/CommentController.php
/controllers/api/v2/CommentController.php
In my routes I call the correct controllers like so:
Route::resource('notification', 'api\v2\CommentController');
This works, but since I'm using namespaces in my controllers, I have to use the \ approach in order to find classes like \Response in the root namespace?
namespace api\v1;
class NotificationController extends \BaseController {
public function index()()
{
return \Response::json({});
}
}
Is there a way to avoid that backslash notation and using the right namespace?
I tried using use \App; but without result
U need to add "use Response;" to your code.
<?php namespace api\v1;
use Response;
class CommentController extends \BaseController {
public function index()
{
return Response::json('hello');
}
}