Route group prefix not working in Laravel 8 - laravel

I was trying to make a group routing with a prefix in Laravel 8. But when I tested it in http://localhost/mysite/admin/test/, it always throws error 404.
Here is the code in web.php:
Route::prefix('/admin', function() {
Route::get('/test', [Admin\LoginController::class, 'index']);
});
I created a controller in app/Http/Controller/Admin/ as the controller is inside Admin folder.
Here is the code in LoginController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function __construct()
{
//
}
public function index()
{
echo "Please login";
}
}
Can anybody show me what I am doing wrong to get it working?

You have to group the routes as stated in the documentation like:
Route::prefix('admin')->group(function () {
Route::get('/test', function () {
// Matches The "/admin/users" URL
});
});
In your case it would be:
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('/test', [LoginController::class, 'index']);
});

I think it should be 'admin' not '/admin'.
That slash makes it:
http://localhost/mysite//admin/test
=>
http://localhost/admin/test
You can check all your routes using: php artisan route:list

Try this
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('test', ['LoginController::class, index'])->name('test'); });

Related

larvael form submit to database giving error

i am new to laravel and i am trying to submit form into data base but i am getting error i dont know why
i have added the screen shot along with that controller
when i do dd($REQUEST->all()) i am getting the form data
<?php
namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $REQUEST){
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/inventory', 'App\Http\Controllers\InventoryController#index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController#sales');
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController#create')->name('invetory.create');
according to your code the controller should look like
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $request){
Inventory::Create($request->all());
}
}
change the namesapce of inventory to use App\Models\Inventory;
and also , inside create function use :
Inventory::Create($request->all());

Illuminate\Contracts\Container\BindingResolutionException Target class [PostsController] does not exist

Can Someone please help me on laravel postcontroller? I am using Laravel 8 but I am seeing below error though my Postcontroller is existing on app>Http>Controllers>PostController.php. Below is my code and I appreciate yoenter image description hereur help in advance.
Routes
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostsController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route:: get('/p/create','PostsController#create');
Route::get('/profiles/{users}',[App\Http\Controllers\HomeController::class, 'index'])->name('home');
PostController
use Illuminate\Http\Request;
class PostsController extends Controller {
public function create() {
return view ('posts/create');
}
}
Insead of :
Route:: get('/p/create','PostsController#create');
use :
Route:: get('/p/create',[PostsController::class , 'create']);
You can read more about laravel 8 routes here:
https://laravel.com/docs/8.x/routing#the-default-route-files

Laravel 8 (version 8.35.1): Target class does not exist

I'm using laravel 8.35.1 version. I have a api-resource controller "ProductController". At my route file api.php. I define the route this:
api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/review', 'App\Http\Controllers\ReviewController');
});
NOTE:
It's work fine but, when i remove the complete path of controller like just write Route::apiResource('/products', 'ProductController'); it show error
Target class [ProductController] does not exist.
Before first clearing the cache. I want to get rid of the complete path. and second want to place the controllers in Api folder, so how to define route for that also.
I have also tried ProductController::class but not work fine
Updated
when I use the route according the laravel 8 doc. https://laravel.com/docs/8.x/controllers#resource-controllers it is working fine. but when move the controller file to Api folder then declare the route name space like use App\Http\Controllers\Api\ProductController; show error again
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', ProductController::class);
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{products}/reviews', ReviewController::class);
});
You dont have to use class on declare controller in routing.
You may change 'ProductController' instead ProductController::class
When i try this then it work fine for me.
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReviewController;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResource('/products', 'App\Http\Controllers\ProductController');
Route::group(['prefix' => 'products'], function () {
Route::apiResource('/{product}/reviews', [ReviewController::class, 'ReviewController']);
});

gives a 404 response when using /admin on the url

I have a problem when using /admin on the url, if I use it always gives a 404 response but, when I change /admin on web.php in other words like adnnin etc. there was no problem
here piece web.php
Route::middleware('isadmin')->group(function () {
Route::prefix('admin')->group(function () {
Route::get('/dashboard', [App\Http\Controllers\Admin\DashboardController::class, 'index']);
});
});
here my controller
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index () {
return view('pages.admin.dashboard');
}
}
Make sure you do not have a folder named admin in your public directory. The webserver will look for folder/files before falling back to handing the request off to the front loader public/index.php, the Laravel application.

Can't access Dingo Api routes in localhost

I have my laravel project created inside laragon/www folder and I'm using dingo/api package for routing.
Here are my files
routes/web.php
<?php
Route::get('/', function () {
return view('welcome');
});
routes/api.php
<?php
use Dingo\Api\Routing\Router;
$api->get('test', 'TestController#index');
app/Http/Controllers/Api/TestController.php
<?php
namespace App\Http\Controllers\Api;
class TestController extends BaseController {
public function index() {
return 'test';
}
}
When I run artisan api/routes it gives me the URI api/test but when I try to reach it in my browser under http://localhost/myproject/public/api/test it returns error "Sorry, the page you are looking for could not be found" knowing that http://localhost/myproject/public/ returns the good result
try
<?php
$api = app('Dingo\Api\Routing\Router');
$api->get('test', 'TestController#index');

Resources