Class App/Http/Controllers/View Not Found error - laravel-5

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.

Related

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

Basic OOPS concept needs clarification

I am new to OOPS and MVC hence, have confusion about certain concepts which I would like to clarify. As mentioned below in the code, I think, namespace App\Http\Controllers\Admin; that is mentioned corresponds to the directory structure and thus means the class AdminController is contained in Admin folder as is pointed there. But then we have the use keyword inorder to use the following namespace. Now the question is why do we use use App\Http\Controllers\Controller;, particular line. What purpose does it serve?
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
namespace App\Http\Controllers\Admin; as you stated is used to let laravel know where to access your AdminController from.
And your AdminController which extends the base Controller uses the default classes of base Controller so we need to use the use specification. use App\Http\Controllers\Controller; is used to let declare that you are going to use the base Controller classes in your AdminController.
For more info read the official laravel docs.
use usually allows a developer to shorten the namespace.
It copies another class from same or different namespace so you can use that class in your code with its class name. You need to write the full namespace if you will use another class without use.
This might help you understand it better. https://daylerees.com/php-namespaces-explained/
namespaces basically group your functions, classes and constants under a particular 'name', which we call a namespace.
Now use keyword allows a developer to shorten the namespace.use is useful when we are going to call the same function again and again in the same code file or call different functions, constants or classes under a particular namespace.

Class 'App\Http\Controllers\PHPExcel_Style_Protection' not found on laravel

In laravel I want protect cell, but a get error Class 'App\Http\Controllers\PHPExcel_Style_Protection' not found, I use MaatWebsite class. this my code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
class ExportExcellDaftarhadirController extends Controller
{
Excel::create('exsport', function($excel) {
$excel->sheet('Daftar', function($sheet){
$sheet->getProtection()->setPassword('password');
$sheet->getProtection()->setSheet(true);
$sheet->getStyle('A12:B20')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
$sheet->loadView('export_excell.daftarhadirujian')
});
})->export('xls');
}
Try using \PHPExcel_Style_Protection::PROTECTION_UNPROTECTED instead of PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
You need to use the properly-namespaced path to PHPExcel_Style_Protection. I don't know the path as I'm unfamiliar with the Maatwebsite library but, as PHP is currently warning you, PHPExcel_Style_Protection isn't found within the App\Http\Controllers namespace.
If you're unfamiliar with namespaces, php's namespace documentation should help clarify their purpose and usage.

Namespacing and autoloading in Laravel

This might be a simple question but I'm wondering how do I autoload useful classes without declaring use statements on every single file.
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
public function index()
{
Input::get('query');
}
}
If I remove the use Illuminate\Support\Facades\Input; line I will get a class not found error because I'm using the Input class.
Is there a way to autoload useful classes like Input, Response, View like in Laravel 4. What's the point of the Aliases in app.php?
You can import input class using both:
use Illuminate\Support\Facades\Input;
or
use Input;
then you can use Input::get('query'); code. That's how PHP namespaces work - you can also look at How to use objects from other namespaces and how to import namespaces in PHP for more details about it.
If you don't use use statement for importing class, you can use \Input::get('query'); or \Illuminate\Support\Facades\Input::get('query');.
Aliases allow you not to use fully qualified classes for example \Illuminate\Support\Facades\Input but shorter form \Input. That's why I showed above 2 versions - the shorter one uses aliases and the longer uses full class path. The same mechanism is both in Laravel 4 and Laravel 5 I believe.
The problem is not really in Laravel, but in PHP. When you namespace a class, it assumes that everything inside that class will be in the same namespace, so you have to tell it that, for a particular class, you need it to use a different namespace.
You can use them by referring to the root namespace, like this:
class HomeController extends Controller
{
public function index()
{
\Input::get('query');
}
}

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