ReflectionException Controller does not exist error in Lumen - laravel

I make Controller in App\Http\Controllers\Controller.php
I use route following code
$app->get('api/article','App\Http\Controllers\ArticleController#index');
But I can't call Controller! Controller does not exist..... error occur.
How can solve?

You only need to specify the namespace relative to App\Http\Controllers. So it would be like this:
$app->get('api/article','ArticleController#index');
Also, for future reference, if your controller is in a "deeper" namespace, the same rule applies. So, if your ArticlesController was in App\Http\Controllers\API\ArticleController, you would just need to do this:
$app->get('api/article', 'API\ArticleController#index');
It is very important to note that we did not need to specify the full controller namespace when defining the controller route. We only defined the portion of the class name that comes after the App\Http\Controllers namespace "root". By default, the bootstrap/app.php file will load the routes.php file within a route group containing the root controller namespace.
If you choose to nest or organize your controllers using PHP namespaces deeper into the App\Http\Controllers directory, simply use the specific class name relative to the App\Http\Controllers root namespace. So, if your full controller class is App\Http\Controllers\Photos\AdminController, you would register a route like so:
$app->get('foo', 'Photos\AdminController#method');
Source: http://lumen.laravel.com/docs/controllers#basic-controllers

Related

Separate Laravel controller for each URL route?

I am building a REST API server (in Lumen, actually, rather than Laravel) with multiple endpoints that allow various operations to be performed on resources such as Users, Accounts, and Products. For example, here are the routes I have defined for the User resource:
GET /v1.0/user
POST /v1.0/user
GET /v1.0/user/{username}
PUT /v1.0/user/{username}
DELETE /v1.0/user/{username}
I currently have all of these API routes for a particular resource defined in a single controller. For example, here are my routes for the User resource:
$router->get('/v1.0/user', 'UserController#listAll');
$router->post('/v1.0/user', 'UserController#createUser');
$router->get('/v1.0/user/{username}', 'UserController#getUser');
$router->put('/v1.0/user/{username}', 'UserController#updateUser');
$router->delete('/v1.0/user/{username}', 'UserController#deleteUser');
Some of the controller logic is getting pretty complex, and I am now finding that my controller files are getting really, really long. I am now thinking that I should use a separate controller file for each route, to make the code more maintainable.
My question is whether there is any idiom or convention I should follow with regard to file/folder naming or structure. Should I create a subfolder under Controllers for each resource (ex: Controllers/User/UserCreateController.php)? Or is this entirely a matter of personal choice?
You should check out Single Action Controller which take only the __invoke() method and can handle one single route.
By the way, what I see usually is that when a controller logic is getting complex, is time to refactor and move that complexity outside the controller.
You do not need to create sub folders or multiple controllers. Use a single controller User Controller which only contains entry points of each route. Move the business logic outside of the controller by creating a class or group of classes that take care of the process.
For example : You can create another directory Libraries under app folder and create a class User under Libraries which contains all functions of User resource.
app/Libraries/User.php
namespace App\Libraries;
class User {}
Now, you can access this class and functions using the namespace within your User Controller
namespace App\Http\Controllers;
use App\Libraries\User;
class UserController extends Controller {}

What is the difference between controller types of Laravel?

I found nothing about definitions/differences between resource and plain controllers.
What is the difference between them?
When you simply create a command with **php artisan:make controller ControllerName** it will create a file with no functions in it. And you can add your functions on your own.
But if you create controller with resource then it will simply give you with all the functions you need for CRUD operation.
And with plain controller you have to create route for each functions. But with resource controller you simply add Route::resource('/routename','ControllerName'); then it will add all the routes for your index,create,store,show,edit,update and delete function.
I hope this answer is helpful for you..
Simply definitions of controllers type is:
Resource controller is used when you perform all CRUD operations.
Plain Controller is used for anything performed manually.
--plain
php artisan make:controller Mycontroller --plain
This will eventually make a plain constructor since you are passing the argument --plain.
The controller that you have created can be invoked from within routes.php file using this syntax below-
Example:- Route::get('base URI','Mycontroller#method');
A basic controller code will look something like this app/Http/Controller/MyController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
//
}
Resource Controllers
The resource route of Laravel allot the classic "CRUD" routes for controllers having a single line of code. This can be created quickly using the make:controller command (Artisan command) something like this"
php artisan make:controller MyController --resource
Actions Handled by Resource Controllers:
Verb URI Action Route Name
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy
More Details:- Resource Controllers

Class App/Http/Controllers/View Not Found error

I am new to laravel 5 and currently stumped by this error:
FatalErrorException in TicketController.php line 18: Class 'App\Http\Controllers\View' not found
Weird thing is the view does in fact exist, i checked to see if the route was indeed routing to the right controller and it was, the error pops up when i try to do this:
return View::make('tickets.bus.index');
It's either i am making some mistake somewhere or if the implementation is different from laravel 4
The problem is not the actual view but the class View. You see when you just reference a class like View::make('tickets.bus.index') PHP searches for the class in your current namespace.
In this case that's App\Http\Controllers. However the View class obviously doesn't exists in your namespace for controllers but rather in the Laravel framework namespace. It has also an alias that's in the global namespace.
You can either reference the alias in the root namespace by prepending a backslash:
return \View::make('tickets.bus.index');
Or add an import statement at the top:
use View;
In Laravel 5.1 the correct use code would be:
use Illuminate\Support\Facades\View;
There exists a helper-function, view(), which is in the global namespace, and may be used to simplify the syntax:
return view('tickets.bus.index');
With this method, it is unnecessary to include use View; or include the root namespace, e.g., \View.
The concepts that lukasgeiter explained are essential to understanding Laravel, even if you elect to use the helper-function.
For me it was namespace problem. I used php artisan to create controller but it seems like php artisan used different namespace (may be I have to change something in composer.json to fix it but I am totally new in laravel)
Whoops, looks like something went wrong.
FatalErrorException in PagesController.php line 11:
Class 'App\Http\Controllers\Controller' not found
Good that I am using phpStorm which automatically inserted proper namespace
make sure you check out namespace properly. This is how I had controller created with php artisan
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller; //php artisan inserted.
class PagesController extends Controller
{
public function index(){
return view('index');
}
public function about(){
return view('pages.about');
}
}
and this is how phpstorm inserted after I manually wrote extends controller
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Routing\Controller; //I manually wrote extends Controller and selected this namespace
class PagesController extends Controller
{
public function index(){
...
There exists a helper-function, view(), which is in the global namespace, and may be used to simplify the syntax:
return view('tickets.bus.index');
With this method, it is unnecessary to include use View; or include the root namespace, e.g., \View.
The concepts that lukasgeiter explained are essential to understanding Laravel, even if you elect to use the helper-function.

Laravel RESTful Controller - Params Before Action Name

I have UsersController which is RESTful controller, there are some functions within it.
inside UsersController.php:
function postOutletVisit($id){
// some code
}
inside routes.php :
Route::controller('users', 'UsersController');
with this route I can access postOutletVisit action like this:
[POST] mydomain.com/users/outlet-visit/{id}
But I'm wondering if it's possible to convert that link to:
[POST] mydomain.com/users/{id}/outlet-visit
I know that I can do this by defining routes for every action like:
Route::post('users/{id}/outlet-visit', 'UsersController#outletVisit')
But this is not suitable for me because there are plenty of actions inside UsersConroller and I will loose the greate naming convenient for actions (first part of action name determines the method used in it, instead of defining the methods separately in routes.php file)
Outlet Visit is a specific resource, so it doesn't belong in your UserController and should have its own controller.
class OutletController extends BaseController
{
public function postStore($userId) // Controller for storing Outlet Visits
{
...
}
}
Then you define your routes for OutletController, specifying that you need to attach it to a specific user :
Route::controller('users/{id}/outlet', 'OutletController');
If needed you can add more actions for this Controller, such as listing, forms for adding / editing, etc.
First off, I think you mean to use Route::resource('users', 'UsersController').
Secondly, of course you can. Just overload the other route method underneath:
// This defines the predefined Laravel routing
Route::resource('users', 'UsersController')
Route::post('users/{id}/outlet-visit', 'UsersController#outletVisit');
There are no ways to change the urls created by the method Route::controller unless you extend it.

Is it possible to load controller from subfolders in laravel?

I need to load controllers/site/user/UserController.php controller from Route.php file.
P.S. I don't need to add automatically it to auto load class
Yes, it is possible. First, you need to namespace the controller, like:
<?php namespace Site\User;
Now, from your routes, you call the methods in UserController by prefixing the namespace of the class like:
Route::get('users', 'Site\User\UserController#index');

Resources