Laravel 5.1 View not found - php

this seems to be an issue that pops up every now and then within Laravel. I was writing a CRUD controller with a view to go with it, however upon testing I got the InvalidArgumentException in FileViewFinder.php line 137: View [bookingcrud.index] not found error. Here is my code:
routes.php:
Route::resource('bookingcrud', 'BookingsCrudController');
BookingsCrudController.php
use uasc\Http\Requests;
use uasc\Http\Requests\CrudCreateRequest;
use uasc\Http\Requests\CrudUpdateRequest;
use uasc\Http\Controllers\Controller;
use Auth;
use DB;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
class BookingsCrudController extends Controller {
public function index()
{
if (!Auth::check() || Auth::user()->authority < 1) {
return redirect('/login');
}
$raw = "select * from bookings";
$bookings = DB::select($raw);
$paginatedBookings = new Paginator($bookings, 1);
return view('bookingcrud.index')->with('bookings', $paginatedBookings);
}
}
And a view located in ~/laravel/resources/views/bookingcrud/index.blade.php
No matter what is in this view file whether its markup from a working view or just the word "cheese" i will always get:
InvalidArgumentException in FileViewFinder.php line 140:
View [bookingcrud.index] not found.
I tested the same view in a known working controller and got the same error however I tested a known working view on the same CRUD controller and it worked. I have also tried changing the directory of the view and renaming it but i'll get the same error with the "View [bookingcrud.index]" changing appropriately. I made sure the permissions of the file and directories were full for testing.
Since first getting the error I have upgraded to 5.1.1 from 5.0.26 (which is the version the error originated on for me) and ran composer update. Also from looking at threads with the same error I have also ran artisan config:clear
I am developing on Windows 8.1 with Homestead 2.0.17 deployed with Virtual Box.
Any help would much appriciated at this point it is doing my head in.

Please use terminal
homestead ssh > Press Enter
vagrant#homestead cd /Path_of_Your_Project/
vagrant#homestead:~/Path_of_Your_project php artisan cache:clear
vagrant#homestead:~/Path_of_Your_project php artisan config:cache
Sorry about my English

Turns out I had spelt blade incorrectly, took a second pair of eyes to notice it though.
$ ls resources/views/crud/booking/
crud.balde.php index.balde.php
Was definitely a lesson to always double check the small things when debugging.
Thanks for the help.

For someone who don't have SSH Access, There are two ways to solve this problem.
If you don't have any plugin of default Laravel package,
First, Just remove the bootstrap/cache/config.php to solved the file,
OR
If you have any plugin of default Laravel package,
Change all related path mentioned bootstrap/cache/config.php into the exact path which allocated the laravel project.

Remember that Linux is case sensitive!
I had something like this:
return view('MyView', $data);
And it was working on my environment (Mac OS), but not on the deployment server (Ubuntu)!

Related

Php Artisan and Tinker not finding laravel project classes

I happened to open two instances of Laragon at the same time. After that php artisan migrate started throwing strange errores like "Interface not found" when that interface is actually there and was correctly imported. Tried different migrations and also throws random errors. I even tried with an old (already migrated) migration, copy pasted the code and it also didn't work. Always related to classes/interfaces not being found.
I've just noted that a simple test:
<?php
use App\Book;
use Illuminate\Database\Migrations\Migration;
class BookTest extends Migration
{
public function up()
{
echo Book::HARDCOVER;
}
}
doesn't work; it says: 'Class 'App\Book' not found' . The book class is there and was imported from phpstorm with a simple click. So, php artisan isn't finding any of my project classes.
I've just confirmed that tinker can't find the classes either.
Ok, I've just noticed that if I change in the book class the namespace to '\App\Models\Store' (where the file actually is) and I do from tinker something like \App\Models\Store\Book::HARDCOVER, then it actually works. The thing I don't get is why it's now (suddenly) needing me to update the name space to work...
Don't know how but my composer version was 2 so I had to downgrade to 1.10 and all works again.

Call to undefined function App\Providers\studly_case() after upgrade to Laravel 6.0.x

As I mentioned in the question, I just upgraded the Laravel version of my project from 5.8.* to 6.0.*.
Now I am getting the below error:
In AppServiceProvider.php line 32:
Call to undefined function App\Providers\studly_case()
Is Laravel supposed to throw error in it's own core file ? I was invoking the following command when I got this error, so I suppose this happens with any artisan command:
php artisan list
I saw in docs, that the new function is Str::studly from Str class file. Is it ok to change in the AppServiceProvider.php ?
Referring to the Laravel 6.x docs, I just discovered that the function for converting the string's current case to studly case is moved to Illuminate\Support\Str class, and named as studly().
So I added that dependency and then replaced the studly_case() to Str::studly() in AppServiceProvider.php, and then was able to run the artisan commands just like before.
I wasn't sure that changing AppServiceProvider.php is standard way or not, but it did worked so I am posting the answer here, that way it'll help anyone else facing the same issue.

I am creating edit page using PHP Laravel 5.8. I have created routes and url, but error 404 not found shows up

I am learning the Laravel by building a job portal app. And I am coding for the company side to edit their post.
Now I could go to url, ex) http://localhost:8000/jobs/23/edit. But on the screen shows me 404|not found error message
This is an screen shot
for edit page, I created views/jobs/edit.blade.php.
Of course, data is clearly inserted in my job table.
I tried to clear all the cache and, executed following commands.
1. composer dump-autoload
2. php artisan clear-compiled
3. php artisan optimize
4. php artisan config:cache
web.php
Route::get('/jobs/{id}/edit', 'JobController#edit')->name('job.edit');
JobController.php
public function edit($id)
{
$jobs = Job::findOrFail($id);
return view('jobs.myjob',compact('jobs'));
}
my-job.blade.php
I have an edit button and following code is a link it has.
{{route('job.edit',[$job->id])}}
My environment is Windows, XAMPP and local MySQL server.
try this way :
Edit Job {{$job->id}}
you can clear route cache by :
php artisan route:clear
update : run following command to clear cache
php artisan config:clear
php artisan cache:clear
I finally could solve this problem.
I changed to a new route.
Route::get('/test-jobs/{id}/edit', 'JobController#edit')->name('test-job.edit');
I created new blade.php like this, views/test-jobs/edit.blade.php
I also changed JobController.php
public function edit($id){
$jobs = Job::findOrFail($id);
return view('test-jobs.edit',compact('jobs'));
}
And finally, my-job.blade.php which has an edit button like this.
<button class="btn btn-dark">Edit</button>
But still, I don't know why it worked.
And also, I think I need another solution. Because in this way, I needed to create another new folder in views.
So if someone knows another solution, please let me know.
Thank you.
The problem is probably in the line return view('jobs.myjob',compact('jobs')); in your JobController.php, it returns the following view: resources/views/jobs/myjob.blade.php (which probably doesn't exist).
try changing the line return view('jobs.myjob',compact('jobs')); to return view('jobs.edit',compact('jobs'));, after the change it refers to resources/views/jobs/edit.blade.php and that should solve your problem.

Call to undefined method Illuminate\Routing\RouteFileRegistrar::get() - Error after upgrading from Laravel 5.7 to 5.8

I have a running app written on Laravel 5.7. I tried to change the record in composer.json to match "5.8.*" and ran composer update. On my local (win10/WAMP) machine it went fine, but on the staging server (Debian 9/nginx) the update command changed the vendor contents and failed at the end.
Since then anything I do with the app on the server I get this error and I can't find any information anywhere.
Call to undefined method Illuminate\Routing\RouteFileRegistrar::get()
And this is the line that fails:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
Thanks in advance!
remove "$this" from your routes and use "Route::"
It is a problem with the routes. Mainly, you get this problem when you are using routes with resource or resources. Make sure you do not have any problem in routes by using the command:
#php artisan route:list
If you are getting any problem while route listing please fix it.
I solved this problem in Laravel 5.8 by fixing routes.
Hope this will help.

Creating a new ServiceProvider / Facade as a package in Laravel 5

Introduction
I've never worked with a framework before (Zend, CakePHP, etc) and finally decided to sit down and learn one. I'm starting with Laravel because the code looks pretty and unlike some other frameworks I tried to install, the "Hello, World!" example worked on the first try.
The Goal
For the time being, I want my app to do something very simple:
User submits a request in the form of: GET /dist/lat,lng
The app uses the remote IP address and MaxMind to determine $latitude1 and $longitude1
This request path is parsed for $latitude2 and $longitude2
Using these two positions, we calculate the distance between them. To do this I'm using Rafael Fragoso's WorldDistance PHP class
Since I plan to re-use this function in later projects, it didn't seem right to throw all of the code into the /app directory. The two reusable parts of the application were:
A service provider that connects to MaxMind and returns a latitude and longitude
A service provider that takes two points on a globe and returns the distance
If I build facades correctly then instead of my routes.php file being a mess of closures within closures, I can simply write:
Route::get('dist/{input}', function($input){
$input = explode( "," , $input );
return Distance::getDistance( GeoIP::getLocation(), $input );
});
What I've tried
Initial Attempt
For the first service provider, I found Daniel Stainback's Laravel 5 GeoIP service provider. It didn't install as easily as it should have (I had to manually copy geoip.php to the /config directory, update /config/app.php by hand, and run composer update and php artisan optimize) however it worked: A request to GET /test returned all of my information.
For the second service provider, I started by trying to mimic the directory structure and file naming convention of the GeoIP service provider. I figured that if I had the same naming convention, the autoloader would be able to locate my class. So I created /vendor/stevendesu/worlddistance/src/Stevendesu/WorldDistance\WorldDistanceServiceProvider.php:
<?php namespace Stevendesu\WorldDistance;
use Illuminate\Support\ServiceProvider;
class WorldDistanceServiceProvider extends ServiceProvider {
protected $defer = false;
public function register()
{
// Register providers.
$this->app['distance'] = $this->app->share(function($app)
{
return new WorldDistance();
});
}
public function provides()
{
return ['distance'];
}
}
I then added this to my /config/app.php:
'Stevendesu\WorldDistance\WorldDistanceServiceProvider',
This fails with a fatal error:
FatalErrorException in ProviderRepository.php line 150:
Class 'Stevendesu\WorldDistance\WorldDistanceServiceProvider' not found
Using WorkBench
Since this utterly failed I figured that there must be some other file dependency: maybe without composer.json or without a README it gives up. I don't know. So I started to look into package creation. Several Google searches for "create package laravel 5" proved fruitless. Either:
They were using Laravel 4.2, in which case the advice was "run php artisan workbench vendor/package --resources"
Or
They were using Laravel 5, in which case the docs were completely useless
The official Laravel 5 docs give you plenty of sample code, saying things like:
All you need to do is tell Laravel where the views for a given namespace are located. For example, if your package is named "courier", you might add the following to your service provider's boot method:
public function boot()
{
$this->loadViewsFrom(__DIR__.'/path/to/views', 'courier');
}
This makes the assumption that you have a service provider to put a boot method in
Nothing in the docs says how to create a service provider in such a way that it will actually be loaded by Laravel.
I also found several different resources all of which assume you have a repository and you just want to include it in your app, or assume you have "workbench". Nothing about creating a new package entirely from scratch.
PHP Artisan did not even have a "workbench" command, and there was no "workbench.php" file in /config, so anything I found related to workbench was worthless. I started doing some research on Workbench and found several different questions on StackOverflow.
After a long time and some experimentation, I managed to get laravel/workbench into my composer.json, composer update, composer install, manually build a workbench.php config file, and finally use the PHP Artisan Workbench command to make a new package:
php artisan workbench Stevendesu/WorldDistance --resources
This created a directory: /workbench/stevendesu/world-distance with a number of sub-directories and only one file: /workbench/stevendesu/world-distance/src/Stevendesu/WorldDistance/WorldDistanceServiceProvider.php
This service provider class looked essentially identical to the file I created before, except that it was in the /workbench directory instead of the /vendor directory. I tried reloading the page and I still got the fatal error:
FatalErrorException in ProviderRepository.php line 150:
Class 'Stevendesu\WorldDistance\WorldDistanceServiceProvider' not found
I also tried php artisan vendor:publish. I don't really know what this command does and the description wasn't helpful, so maybe it would help? It didn't.
Question
How do I create a new service provider as a package so that in future projects I can simply include this package and have all the same functionality? Or rather, what did I do wrong so that the package I created isn't working?
After two days of playing with this I managed to find the solution. I had assumed that the directory structure mapped directly to the autoloader's path that it checked (e.g. attempting to access a class Stevendesu\WorldDistance\WorldDistanceServiceProvider would look in vendor/stevendesu/world-distance/WorldDistanceServiceProvider)... This isn't the case.
Reading through the composer source code to see how it actually loads the files, it builds a "classmap" - essentially a gigantic array mapping classes to their respective files. This file is built when you run composer update or composer install - and it will only be built correctly if composer knows the details of your package. That is - if your package is included in your project's composer.json file
I created a local git repository outside of my app then added my package to my app's composer.json file then ran composer update -- suddenly everything worked perfectly.
As for the:
It didn't install as easily as it should have
the secret sauce here was first add the service provider to /config/app.php then, second run php artisan vendor:publish

Categories