I face a problem while trying to create a router under namespace "Instructor" as per the image below:
Route in instructor page in routes file
and the method under the namespace Instructor in the controller is:
method inside the controller file
it keeps giving me the following error when running the route:
Error message
can anyone help me solve this issue as I am new to larevel so I am not a pro in defining the packages and dependencies.
In Laravel 8, you can simply do the following in your route file:
Route::get('instructors', [\App\Http\Controllers\Instructor\FirstController::class, 'showUserName']);
Or with quotes:
Route::get('instructors', ['\App\Http\Controllers\Instructor\FirstController', 'showUserName']);
Or with use statement:
use App\Http\Controllers\Instructor\FirstController;
Route::get('instructors', [FirstController::class, 'showUserName']);
You can then have all your use statements at the top. Most IDE can automatically hide them so you have a clean route file.
In Laravel 8 you can do:
Route::get('your-route', [App\Http\Controllers\Instructor\YourController::class, 'methodName']);
Or you can uncomment following line in RouteServiceProvider
// protected $namespace = 'App\\Http\\Controllers';
Related
I try to create routes to my index.blade.php page, i've made a controller "ProductController" using cmd php artisan make:controller ProductController, so in http --> Controllers i do have a ProductController.php file and i put this code in it :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductContoller extends Controller
{
public function index()
{
return view('products.index');
}
}
and then in my web.php i create a route using this code
Route::get('/boutique', 'ProductController#index');
However it doesn't work.
Firstly, when i go to the pretty url i've setup for my project at localhost using Laragon --> Projetname.test i get the normal Laravel Welcome page, but when i try to go to the url i've just setup like : ProjectName.test/boutique, i get
"Target class [App\Http\Controllers\ProductController] does not exist."
After reading about the changes since the update of Laravel to V8 here, i've seen that the update made some requirements for routes since $namespace prefix are not enabled automatically, but however that can be enabled again by uncommenting this line in RouteServiceProvider
// protected $namespace = 'App\\Http\\Controllers';
I do uncommenting that line and then clear the cache using php artisan route:cache, however it still not working..
When i first started doing research about routes issues in Laravel i've seen many forum spotted out that apache Allowoverride settings in httpd.config file may cause issue, so i change it settings from None to All and then restart Laragon but nothing works.
After correcting label on my controller it still do not work, i try both methode (the old and the new one) none of them works for me, cmd keeps returning me :
λ php artisan route:list
Illuminate\Contracts\Container\BindingResolutionException
Target class [ProductController] does not exist.
at C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:835
831▕
832▕ try {
833▕ $reflector = new ReflectionClass($concrete);
834▕ } catch (ReflectionException $e) {
➜ 835▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
836▕ }
837▕
838▕ // If the type is not instantiable, the developer is attempting to resolve
839▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console\{closure}(Object(Illuminate\Routing\Route))
2 C:\laragon\www\ProjectName\vendor\laravel\framework\src\Illuminate\Container\Container.php:833
ReflectionException::("Class "ProductController" does not exist")
Laravel 8 Route should be
Route::get('/boutique', [NameController:class,'method']);
So in your web.php file add
use App\Http\Controllers\ProductContoller
Then write your route like this:
Route::get('/boutique', [ProductContoller::class,'index']);
And I think there is a missing 'r' in your "ProductController" class name
I found out after watching a tutorial here on most common issues with new routing methodes on Laravel 8 that when uncommenting the RouteServiceProvider, using the old method require to use the old route method too on web.php, so it looks like that :
Route::get('/boutique', 'ProductController#index');
Please use
Route::get('/boutique', '\App\Http\Controllers\ProductController#index');
or use name route group and indicate the namespace
Route::group(['namespace' => 'App\Http\Controllers'], function(){
Route::get('/boutique', 'ProductController#index');
});
let me know how it works.
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
I am trying to use the slim framework to build a website, however I get the following error: "Cannot call index on Store\HomeController because it is not a class nor a valid container entry." I have thoroughly checked for spelling and punctuation errors and found none. The error go's to line 98 of a "callableResover.php file of the following code:
throw new NotCallableException(sprintf(
'Cannot call %s on %s because it is not a class nor a valid container entry',
$callable[1],
$callable[0]
));
This is the HomeController.php file the I created as the following:
namespace Store\Controllers;
class HomeController{
public function index(){
echo('Index');
}//end function index
}//end class
And this the route.php file of the code following:
$app->get('/', ['Store\HomeController', 'index'])->setName('home');
Assuming your HomeController file is loaded before using it in your route.php file, try to:
1. Add a \ to your controller's namespace in the route definition
\Store\HomeController
2. Change your route to this one instead
$app->get('/', \Store\HomeController::class . ':index');
Check the Slim documentation to learn more about container resolution
I found a problem..
The class is probably registred in /vendor/composer/autoload_classmap.php
so you have to regenerate it with command
composer dump-autoload
and it should work...
I am very new to Laravel and followed this tutorial for the startup.
As in the tutorial, i have
Route::get("contact","WelcomeController#contact");
and in the Welcomecontroller I have a contact method:
public function contact()
{
return "Contact page";
}
But accessing the page http://localhost/laravel.dev/contact throws me NotFoundHttpException in D:\wamp\www\laravel\vendor\compiled.php line 7693:
What could be the reason behind this? Is there something to do with setting or installtion path?
I'd try two things:
Make sure you use the same cases for controller class name and route declaration (WelcomeController might not be equal to Welcomecontroller).
Go to your project base dir and run "php artisan clear-compiled" to make sure your Laravel "recompiles" everything and your compiled.php file includes the ControllerClass your route is expecting.
More things to check:
Question first: When you setup Laravel, did you test the installation to see that it worked? Or is this the first route you have tested?
If this is the very first route you have tested, maybe you have not set up the htaccess properly. See here: http://laravel.com/docs/4.2#pretty-urls
You need to have mod_rewrite enabled.
Otherwise:
1) Check to make sure that inside WelcomeController.php you have named your class WelcomeController (if you were copying an existing example, you may not have remembered to rename the class)
2) Like MaGnetas answered, make sure that in ALL instances, you have used the same spelling and same lower/upper case for "WelcomeController" (inside the routes, inside your class, anywhere you reference it.
I am getting an error message when trying to register all the controller routes in Laravel 4 (Illuminate) by adding:
Route::controller(Controller::detect());
to my routes.php
The error :
Error: Call to undefined method Illuminate\Routing\Controllers\Controller::detect() in C:\wamp\www\travless\app\routes.php line 13
I suppose they changed the function name, but I don't know where to find it because it is still an alpha version and there is no documentation I'm aware of.
This function has been removed in Laravel 4 because of inconsistent behavior with varying filesystems. The proper way to register controllers should be to explicitly define each one you wish to use in your routes file.
You need to register each controller manualy in routes.php file
Route::controller('users', 'UsersController');
First params stands for URL to respond, second one is controller's class name