I'd been working on my portfolio development with a blog alongside on Laravel 5.1.2 lately. It all was working fine till the moment I needed the illuminate/html package. Also, prior to running the composer require illuminate/html command, I ran the composer update command to let all the libraries update to their newer versions.
But that's where the problem crept in. Since, the upgrade to Laravel 5.1.7 and the installation of the illuminate/html package, the project has gone haywire and has been throwing barking mad
ReflectionException in Container.php line 736:
Class view does not exist
And I haven't the slightest clue where this is arising from.
PS: I have also updated the app.php file to include the respective providers and aliases corresponding to illuminate/html.
Update
Here's the code from my controller files if it might be of any help.
PagesController.php (I generated it as a plain stub via the --plain artisan option)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
public function index()
{
return view('pages.home');
}
}
ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$articles = Article::all();
return view('blog.index', compact('articles'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('blog.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$article = Article::findOrFail($id);
return view('blog.show', compact('article'));
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
Related
I am using Laravel 7.0.
I have created a new module and in the new modules RouteServiceProvider.php file changed the prefix of the API routes to cp.
I am now trying to send some query parameters but nothing is received in the controller action method.
Here you can see the Laravel Telescope also not showing any query parameters. I checked the Nginx logs and query parameters are present there.
php artisan route:list is showing the route correctly.
Here is the code of the controller.
<?php
namespace Modules\SomeModule\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class PartnerController extends Controller
{
/**
* List all Partners
*
* #param Illuminate\Http\Request $request
* #return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$page = $request->query('page'); // it is always null
$pageSize = $request->query('page_size'); // it is always null
return response()->json(['page' => $page, 'page_size' => $pageSize]);
}
}
Here is the code of RouteServiceProvider. I am using nWidart/laravel-modules package to generate modules in my app.
<?php
namespace Modules\SomeModule\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* The module namespace to assume when generating URLs to actions.
*
* #var string
*/
protected $moduleNamespace = 'Modules\SomeModule\Http\Controllers';
/**
* Called before routes are registered.
*
* Register any model bindings or pattern based filters.
*
* #return void
*/
public function boot()
{
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::middleware('api')
->prefix('cp')
->namespace($this->moduleNamespace)
->group(module_path('SomeModule', '/Routes/api.php'));
}
}
When I changed the API prefix back to api it works absolutely fine.
Can someone please guide what is going on here? Thanks!
I am little new to this.
I have deployed a laravel project on hostinger shared hosting.I have hosted and tested with postman it was working fine (AdminController). today i have added few more new routes(PatchController & CustomerController-Both Not Working) and just uploaded files and folder directly to their respective location below Code and server directory stucture's pictures has been also attached for your reference:
app/Http/Controllers/Patch/PatchController.php
<?php
namespace App\Http\Controllers\Patch;
use App\Providers\RouteServiceProvider;
//use Closure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Patch;
use Illuminate\Support\Facades\Hash;
class PatchController extends Controller
{
public function index()
{
//
return Patch::all();
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$patch= Patch::where('patch_name', $request->patch_name)->first();
// print_r($data);
if (!$patch) {
$request->validate([
'patch_name'=> 'required',
'emp_id'=>'required',
's_location'=> 'required',
'e_location'=> 'required',
'p_status'=> 'required'
]);
return Patch::create($request->all());
}
return response([
'message' => ['This patch already exist.']
], 404);
}
/**
* Display the specified resource.
*
* #param \App\Models\patch $patch
* #return \Illuminate\Http\Response
*/
public function show($id)
{
return Patch::where('patch_id',$id)->get();
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\patch $patch
* #return \Illuminate\Http\Response
*/
public function edit(patch $patch)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\patch $patch
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$product= Patch::find($id);
$product->update($request->all());
$result=$product->save();
if($result)
{
return ['result'=>'Data has been updated'];
}
else
{
return ['result'=>'update operation has been failed'];
}
return $product;
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\patch $patch
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$product= Patch::find($id);
$result=$product->delete();
//$result=$product->save();
if($result)
{
return ['result'=>'Data has been deleted'];
}
else
{
return ['result'=>'update operation has been failed'];
}
return $product;
}
}
app/Models/Patch.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class Patch extends Model
{
use HasFactory;
protected $fillable=[
'patch_name',
'emp_id',
's_location',
'e_location',
'p_status'
];
}
And I have replaced the old api.php with the below code.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Patch\PatchController;
use App\Http\Controllers\Customer\CustomerController;
//use App\Models\Patch;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['middleware' => 'auth:sanctum'], function(){
//All secure URL's
Route::get('/admins',[AdminController::class,'index']);
Route::post('/store',[AdminController::class,'store']);
Route::put('/update/{id}',[AdminController::class,'update']);
Route::get('/search/{id}',[AdminController::class,'show']);
Route::delete('/delete/{id}',[AdminController::class,'delete']);
// AdminController is working fine
Route::get('/patches',[PatchController::class,'index']);
Route::post('/c_patch',[PatchController::class,'store']);
Route::put('/u_patch/{id}',[PatchController::class,'update']);
Route::get('/s_patch/{id}',[PatchController::class,'show']);
Route::delete('/d_patch/{id}',[PatchController::class,'destroy']);
// PatchController is not working.
//Route::get('/patches',[PatchController::class,'index']);
Route::post('/c_customer',[CustomerController::class,'store']);
//Route::put('/u_patch/{id}',[PatchController::class,'update']);
//Route::get('/s_patch/{id}',[PatchController::class,'show']);
//Route::delete('/d_patch/{id}',[PatchController::class,'destroy']);
// CustomerController is not working.
});
Route::post("login",[AdminController::class,'log']);
Route::get('clear', function() {
Artisan::call('optimize:clear');
return redirect()->back();
//return view('welcome');
});
/*
Route::post('loginWithOtp', 'UserController#loginWithOtp')->name('loginWithOtp');
Route::get('loginWithOtp', function () {
return view('auth/OtpLogin');
})->name('loginWithOtp');
*/
**Note:
Same code is working fine on localhost but throwing a 404 error on live server with postman.
AdminController is working fine but PatchController & CustomerController giveing 404 Not Found error. Any help with highly appreciated**
Server Directory Structure:
Models Path
Controllers Path / Folder Structure
Server Folder Structure
Server Folder Stucture
Have you tried delete cached files?
Delete everything in bootstrap\cache and try again on host.
You might cached routes to optimize speed and when you cache routes, you should generate new file to routes be accessible, because laravel use cached file instead of looking for api.php and web.php.
So I have two models User and File, these are connected with a one-to-many relationship.
I have sorted the API routes and controllers to index all users, show specific user and index all files uploaded by that specific user. I do not know how to write the logic that will allow this route 127.0.0.1:8001/api/2/files/1 to show the first file uploaded by the 2nd user. So/2(seconduser)/files(shows all)/1(shows only 1 file)
This is my API code:
Route::group(["prefix" => "/"], function () {
Route::get("", [Users::class, "index"]); //show all users
Route::group(["prefix" => "{user}"], function () {
Route::get("", [Users::class, "show"]); //show specific user
Route::group(["prefix" => "/files"], function () {
Route::get("", [Files::class, "index"]); //show all files
Route::group(["prefix" => "{file}"], function () {
Route::get("", [Files::class, "show"]); //trying to show specific file
});
});
});
});
Files Controller
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\File;
class Files extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(User $user)
{
return $user->files;
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(User $user, File $file)
{
}
}
Users Controller
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
class Users extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return User::all();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show(User $user)
{
return $user;
}
}
So here's what a typical route declaration would look like. Note the user ID is not relevant to the file request, so the files endpoint is made separate from the users endpoint.
Route::get("/users", [Users::class, "index"]);
Route::get("/users/{user}", [Users::class, "show"]);
Route::get("/users/{user}/files", [Files::class, "index"]);
Route::get("/files/{file}", [Files::class, "show"]);
And then in your controller methods, you're simply returning a list or a single item, mostly as in your original code. Note if you're returning API data, you should explicitly return JSON.
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\File;
class Files extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\JsonResponse
*/
public function index(User $user)
{
return response()->json($user->files);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\JsonResponse
*/
public function show(File $file)
{
return response()->json($file);
// or perhaps something like this?
return response()
->download($file->path, $file->name, ["Content-Type" => $file->type]);
}
}
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
class Users extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\JsonResponse
*/
public function index()
{
return response()->json(User::all());
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\JsonResponse
*/
public function show(User $user)
{
return response()->json($user);
}
}
I'm am new to laravel and i am trying to get my custom validation rules to work on my controller.
It's showing that the class does not exist.
ReflectionException thrown with message "Class App\Http\Controllers\StoreBooksRequest does not exist"
I made the request file using the artisan command.
lando artisan make:request StoreBooksRequest
this is my request file :
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreBooksRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'title' => 'required|unique:books|max:150',
'description' => 'required',
'isbn' => 'required|max:20'
];
}
}
and this is the controller where i am trying to get the custom request rules to work :
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Request;
class BooksController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$books = Book::all();
return view('books.index', compact('books'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('books.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(StoreBooksRequest $request)
{
$book = new Book();
$book->title = $request->title;
$book->description = $request->description;
$book->isbn = $request->isbn;
$book->save();
}
I think the problem is with the error saying that the request file is in the Controllers folder and not in the standard Requests folder.
You have not included the namespace of your custom request's class. Add use App\Http\Requests\StoreBooksRequest; after use Illuminate\Http\Request;
You seem to be using wrong namespace for your
Class App\Http\Controllers\StoreBooksRequest
Your namespace is set to namespace App\Http\Requests; while you are calling it from controller, If you move your Class to App\Http\Requests.
Also, don't forget to import the class in your controller
use StoreBooksRequest
When you execute the php artisan make:request Myrequestname, Laravel create the file inside the App\Http\Request subdirectory, so you need to be careful to use the right namespace, another thing you always had to be carefull is about the name you use, is not the same Mycontroller than mycontroller and is worst if your server is a Linux server, because the file system make differentiation beewteen Caps.
I am trying to understand the packages in laravel which I should be able to upload to github subsequently.
I got everything working except for the controller of my package, haven't worked on views yet.
But I am getting the following error when I visit my localhost website http:\guam.dev\people
ReflectionException
Class Dashboard does not exist
This is my Directory structure.
laravel
-vendor
--student
---people
----composer.json
----src
-----PeopleServiceProvider.php
-----Controllers
------SuperAdmin
-------Dashboard.php
-----Routes
-----Views
This is my PeopleServiceProvider.php
namespace Student\People;
use Illuminate\Support\ServiceProvider;
class PeopleServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
include __DIR__ . '/Routes/routes.php';
$this->loadViewsFrom(__DIR__.'/Views/SuperAdmin/Dashboard', 'People');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->make('Student\People\Dashboard');
}
}
Controller file : Dashboard.php
namespace Student\People;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Dashboard extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('app');
}
}
What am I doing wrong ? I am following an old tutorial, but there isn't much explanation about controllers there.