Laravel routes not moving to the next route - php

I have two routes
Route::get('events{event}', [EventController::class, 'show']);
Route::get('events/{eventType:slug}', [EventTypeController::class, 'show']);
and in the event controller
public function show(Event $event)
{
return $event;
}
Basically I expect to have both GET /events/1 and GET /events/slug working which it does with the above.
Now I would like to refactor the above routes to
Route::resource('events', EventController::class);
Route::get('events/{eventType:slug}', [EventTypeController::class, 'show']);
Now I receive Error: 404 NOT FOUND. Why isn't resources route proceeding to the next route?
If I change the above to below GET /events-types/my_slug works as expected
Route::resource('events', EventController::class);
Route::get('events-types/{eventType:slug}', [EventTypeController::class, 'show']);

Related

using Laravel 7 Api routes

I'm trying to use simple laravel api for getting and sending requests, after define this api routes in api.php:
Route::prefix('Api/v1')->group(function () {
Route::any('login', 'Api\v1\AuthController#login');
Route::any('register', 'Api\v1\AuthController#register');
});
and creating AuthController in app/http/controller/Api/v1 directory:
class AuthController extends Controller
{
public function login()
{
dd(request()->all());
}
public function register()
{
dd(request()->all());
}
}
i get 404 error on this link:
http://127.0.0.1:8000/Api/v1/login
how can i resolve this problem?
Routes in api.php are automatically prefixed with /api. Currently, your routes are:
http://127.0.0.1:8000/api/Api/v1/login
http://127.0.0.1:8000/api/Api/v1/register
So navigating to http://127.0.0.1:8000/Api/v1/login is a 404.
If you remove /Api, and just use Route::prefix('/v1') ... then you should have no issue.
Also, always double check your routes with php artisan route:list to see what's wrong.
The API Routes are already prefixed by /api . I think the correct structure you'd looking for would be
Route::prefix('v1')->group(function () {
Route::any('login', 'AuthController#login');
Route::any('register', 'AuthController#register');
});
This way, you're calling the methods Login and Register from you /Controllers/AuthController file with the route
http://127.0.0.1:8000/api/v1/login
You can use many ways to define routes for API in laraval > routes > api.php file.
In this i'm going to explain how we can use routes group in the laraval..
Route::group([
'namespace' => 'Customers', //namespace App\Http\Controllers\Customers;
'middleware' => 'auth:api', // this is for check user is logged in or authenticated user
'prefix' => 'customers' // you can use custom prefix for your rote {{host}}/api/customers/
], function ($router) {
// add and delete customer groups
Route::get('/', [CustomerController::class, 'index']); // {{host}}/api/customers/ this is called to index method in CustomerController.php
Route::post('/create', [CustomerController::class, 'create']); // {{host}}/api/customers/create this is called to create method in CustomerController.php
Route::post('/show/{id}', [CustomerController::class, 'show']); // {{host}}/api/customers/show/10 this is called to show method in CustomerController.php parsing id to get single data
Route::post('/delete/{id}', [CustomerController::class, 'delete']); // {{host}}/api/customers/delete/10 this is called to delete method in CustomerController.php for delete single data
});
You can create controller using artisan command with default methods
php artisan make:controller Customers/CustomerController --resource

Laravel: How to fix routing the index page is not defined, but the routing declared already on the controller?

I got confuse and I got an error, why does my routing process not work, The error gives me Route [index] not defined, but on other hand I already defined the index to HomeController, take a look at my process that I did,
Note: I used laravel version: 5.8*
I create a index.blade.php
Add the routes to the web.php and I used this code
`Route::get('/index', 'HomeController#index');
I add the public function index to the HomeController
Web.php
Route::get('/index', 'HomeController#index');
HomeController
public function index()
{
return view('index');
}
My URL:
Error:
The problem might be in your index view.
Looks like you are trying to access route using route name and you have not defined the route name for index route.
So in web.php add ->name('index')
Route::get('/index', 'HomeController#index')->name('index');
You have to provide name of the route in your routes.
Route::get('/index', 'HomeController#index')->name('index');
You can also use the below syntax
Route::get('/index', [
'as' => 'index',
'uses' => 'HomeController#index'
]);
for more information please have a look at the docs
https://laravel.com/docs/5.7/routing#named-routes
Try This
if you use route this way
Route::get('/index', 'HomeController#index');
//then your url will be
URL/index
OR use this way
Route::get('/', 'HomeController#index');
//then your url will be
URL
Try with localhost/folder_name/public/index if this works for you then probably problem is with virtual host creation.
Somewhere in your view you are using {{route('index')}}.
Add ->name('index') in the end of your route.
Route::get('/index', 'HomeController#index')->name('index');
Hope this will help.

laravel 5.5 error in routes - InvalidArgumentException Route [login] not defined

i replace my web.php whit this code, same as my code in laravel 5.2, now im using laravel 5.5, i dont have any errors in 5.2 version.
Route::get('/home', function () {
return view('home');
});
Route::get('/register', 'registerController#index');
Route::post('/register', 'registerController#register');
Route::get('/signin', 'signinController#index');
Route::post('/login', 'signinController#login');
Route::get('/logout', ['uses'=>'signinController#logout'])->middleware('auth');
Route::get('/profile', ['uses'=>'profileController#index'])->middleware('auth');
Route::get('/updateprofile', ['uses'=>'profileController#updateprofile'])->middleware('auth');
Route::post('/updateprofile', ['uses'=>'profileController#updateprofilesave'])->middleware('auth');
Route::post('/updateprofiles', ['uses'=>'profileController#updatechannelart'])->middleware('auth');
Route::get('/changepassword', ['uses'=>'profileController#indexpassword'])->middleware('auth');
Route::post('/changepassword', ['uses'=>'profileController#changepassword'])->middleware('auth');
Route::get('/article', 'articleController#index');
Route::get('/searchuser', ['uses'=>'searchController#index']); //Untuk searching user
Route::get('/searchuserpage', ['uses'=>'searchController#searchuser']); //searching user jquery
Route::get('/photos', ['uses'=>'documentationController#indexphoto'])->middleware('auth');
then i try to access url /profile which means need authenticate first, and it show me an error InvalidArgumentException Route [login] not defined. how to solve this problem. thankyou
this is my code for Authenticate.php
public function handle($request, Closure $next)
{
if(Auth::Check()){
return $next($request);
}
else{
return redirect('/signin');
}
}
The issue comes from the fact that somewhere in your code upon instantiation you're referring to a named route called 'login' but it's not defined in your web.php file.
An example of hitting this issue is you may have a redirect pointing to this route somewhere tucked away in one of your controllers, for example:
return redirect()->route('login');
To fix this issue, apply the name to the applicable route.
Route::post('/login', 'signinController#login')->name('login');
when you call a route in your project you must define route name .
such as :
<form action:"{{route('login')}}" method="post">
and in route :
Route::post('/signin', 'signinController#index')->name('login')
This is issue with the named routes. Please make sure which all places the named routes is being used.
Route::get('/signin', 'signinController#index')->name('login')
Here, you can see I named this route login and I can call this route anywhere using route('login') helper method.

laravel catch all route as last option

I am not sure the best way to do this but basically I have some routes set up and some vendor ones as well but my kind of "catch all" route is getting called when I need the vendor "/forum" to be used.
Here are my routes:
Route::get('/', function () { return view('welcome'); });
Route::get('/contact', function () { return view('contact'); });
Route::get('/login', function () { return view('login'); });
Route::get('/signup', 'UserController#create');
Route::get('/logout', 'UserController#logout');
Route::get('/{slug}', 'PageController#show');
You can see the last route basically just gets the slug and and then in the controller I return the page by slug. The issue is with /forum the PageController#show is getting called since I assume Laravel looks at this route file before the vendor. Is there a better way of setting it up so Route::get('/{slug}', 'PageController#show'); gets called as the last possible option after vendor routes as well?
Laravel routes are loaded in order they are defined so the only way to have your /forum match before /{slug} is to make sure that route is loaded first. To guarantee it is loaded last I would suggest adding it after all the other routes are loaded in app/Providers/RouteServiceProvider.php which would look like:
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
Route::get('/{slug}', 'App\Http\Controllers\PageController#show');
}

Laravel 4 Controller Route - Get Route with Numerical Parameters and Other Specific Parameters

Looking for some insight on this.
I am trying to define a route controller in a Laravel 4 application that does the following things.
Handles the route user/{userid}
Handles the route user/new
I cannot figure out a way to make both happen simultaneously.
My controller and route look like this:
Route::controller('user', 'UserController');
class UserController extends BaseController {
public function getIndex($user_id) {
$user = User::find($user_id);
return View::make('user')
->with('user', $user);
}
public function getNew() {
return View::make('create_user');
}
}
To me, that should work, but I get a 404 when trying to go to user/1. Oddly enough, user/new still takes me to create_user as it should. The user.blade.php view and create_user.blade.php view both exist in the root of views, so that isn't the problem either. What am I doing wrong?
Instead of doing Route::controller('user', 'UserController'); you can do the following in your routes:
Route::get('user/new', 'UserController#getNew');
Route::get('user/{user_id}', 'UserController#getIndex');
OR you can also do the following:
Route::get('user/{user_id}', 'UserController#getIndex');
Route::controller('user', 'UserController');

Categories