This question already has an answer here:
Laravel route is bringing me somewhere else
(1 answer)
Closed 5 months ago.
I am trying to create a crud app with laravel 9 and vuejs but when I run the app it goes to the dashboard and the crud operations don't appear. I want it to directly goes to the crud operations. Here is the web.app route codes:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return Inertia::render('Welcome', [
'canLogin' => Route::has('login'),
'canRegister' => Route::has('register'),
'laravelVersion' => Application::VERSION,
'phpVersion' => PHP_VERSION,
]);
});
Route::get('/dashboard', function () {
return Inertia::render('Dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
require __DIR__.'/auth.php';
Route::resource('posts', PostController::class);
The Route::resource function creates the routes for you with defined paths shown in the link here:
https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller
If you want to define the endpoint for going directly to posts but want to keep the rest of the default resources you can do something like this:
Route::resource('posts',PostController::class)->except(['index']);
Route::get('/dashboard',[PostController::class,'index'])->name('dashboard');
And comment out the other Dashboard link
IF you want to change the home link, you can open the RouteServiceProvider in App/Providers/RouteServiceProvider and update it there to "/posts" which would be much faster and less confusing.
public const HOME = '/posts';
Related
I know there is a heck ton of links out there about Laravel not finding some class and stuff, but I really tried everything here and nothing works. I donĀ“t even know which code I should share here to help, so...I'll share my Newsletter controller and the web.php code
I'm using laravel 9.9 and php 8.1
web.php
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsletterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('newsletter','NewsletterController#create');
Route::post('newsletter','NewsletterController#store');
Newsletter controler
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Newsletter;
class NewsLetterController extends Controller
{
public function create()
{
return view('newsletter');
}
public function store(Request $request)
{
if ( ! Newsletter::isSubscribed($request->email) )
{
Newsletter::subscribePending($request->email);
return redirect('newsletter')->with('success', 'Thanks For Subscribe');
}
return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');
}
}
The issue is that your class name has a capital L for Letter, while on the route it does not, so your web.php should look like this:
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsLetterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('newsletter','NewsLetterController#create');
Route::post('newsletter','NewsLetterController#store');
Maybe I am not sure
I think you can't use this method to write route in laravel 9 ...because I am using larvel 8 and it is not working so better to use:
Route::get('/usernewsletter, [NewsLetterController::class, 'create']);
https://laravel.com/docs/9.x/routing
I have a quick question I know it wouldn't take so much time to fix but somehow I don't seem to easily find the solution.
I am building a basic api for a mobile application. I placed by api routes in api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/v1/meeting/record', 'App\Http\Controllers\Attendance#record');
The problem is anytime I send a request to this route it responds with page status 419 (Page Expired).
This is my record method in the Attendance Controller
public function record(Request $request)
{
return response()->json([
"message" => "student record created"
], 201);
}
I added the api/* to the excludes in verifycsrftoken.php but it didn't change anything.
Am I doing anything wrong?
php artisan route:cache
This has fixed it for me in the past.
Here is a discussion on all of the ways to clear cache:
https://dev.to/kenfai/laravel-artisan-cache-commands-explained-41e1
import your controller at the top of the controller class
App\Http\Controllers\Attendance;
then define your route as such
Route::post('/v1/meeting/record', [Attendance::class,'record']);
try this and let me know. thanks
This question already has an answer here:
Updates to Laravel route file have no effect
(1 answer)
Closed 1 year ago.
While being on learning curve with laravel, I have created the new project and installed jetstream on it, have experimented with preprocessor's configurations, and some other basic stuff. Right now when I have added the simplest it does not work:
Route::get('foo', function () {
return 'Hello World';
});
And all the previously added routes work fine.
Here is the whole web.php file:
<?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('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('/testcomp', function(){
return view('testcompmain');
});
Route::get('laravelProj3/login', function(){
return view('laravelProj3.auth.login');
});
Route::get('foo', function () {
return 'Hello World';
});
If any other files are helpful here, please let me know I will post them.
Is there any way to reset, or rebuild the project to force it to work again?
What if something like this happens while I am working on real project?
Can I somehow automatically copy all files that I added to newly build working application?
Is there any way except debugging to find where is the problem?
Update #1: Here is what I have found while debugging:
"Exception has occurred.
Symfony\Component\Routing\Exception\ResourceNotFoundException: No routes found for "/foo"."
Your route has been cached. It reduces all of your route registrations into a single method call within a cached file, improving the performance of route registration when registering hundreds of routes. If you want to clear the cached file, then simply hit :
php artisan route:clear
You can cache your route again by :
php artisan route:cache
For more info see the official documentation of Route Caching, Optimizing Views, Environment
I'm writing my project on Laravel. When I optimize the project, I have a problem :
Unable to prepare route [api/user] for serialization. Uses Closure.
I looked for any closures in web.php, but I didn't find anything
<?php
/*
|--------------------------------------------------------------------------
| 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('/','ReviewsController#main')->name('main');
Route::post('/','MailController#verify')->name('verifyPost');
Route::get('/reviews', 'ReviewsController#index')->name('reviews');
Route::post('/reviews','ReviewsController#add')->name('addReview');
Auth::routes();
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
in api.php file search and comment this route you will not get error..
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
and also in web.php file route::group is also closure and also comment them for test
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
see what is closure
Php routing cache command :
php artisan route:cache
if your application using controller based routes. It help for fast execution. But remember "Closure based routes cannot be cached"
So kindly convert your Closure routes to controller classes.
For more information
Make sure to Check "routes/api.php"
I have a problem, new routes in laravel are not working, url shows the correct route but almost as if it does not get to my routes web file just returns page not found every time.
I have tried:
using named route,
moving function to different controller,
clearing route cache,
clearing app cache,
dump-auto load,
made sure that AllowOverride is set to All,
Web.php:
<?php
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/
Route::get('/courses', 'CourseController#index');
Route::get('/courses/create', 'CourseController#create');
Route::get('/courses/{course}', 'CourseController#show');
Route::get('/courses/{course}/edit', 'CourseController#edit');
Route::post('/courses', 'CourseController#store');
Route::patch('/courses/{course}', 'CourseController#update');
Route::delete('/courses/{course}', 'CourseController#destroy')->name('course-delete');
Route::get('/courses/statistics', 'CourseController#statistics');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/section/{section}', 'SectionController#show');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/progress', 'UserProgressController#index');
Route::get('/progress/create', 'UserProgressController#create');
Route::get('/progress/{section}', 'UserProgressController#show');
Route::get('/progress/formativeresults', 'UserProgressController#formativeresults');
//Route::get('/progress/coursestatistics', 'UserProgressController#coursestatistics');
//Route::get('/progress/{progress}/edit', 'UserProgressController#edit');
Route::post('/progress', 'UserProgressController#store');
//Route::patch('/progress/{progress}', 'UserProgressController#update');
//Route::delete('/progress/{progress}', 'UserProgressController#destroy')->name('progress-delete');
Controller:
public function statistics()
{
dd('Test');
return view('coursestatistics');
}
View file name:
coursestatistics.blade.php file structure views/coursestatistics
Link to page:
<a class="navbar-brand" href="/courses/statistics">
{{ __('Statistics') }}
</a>
Can anyone tell me what might be causing route not to work?
Try placing
Route::get('/courses/statistics', 'CourseController#statistics');
below this particular line of route code
Route::get('/courses/create', 'CourseController#create');
The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here
I had same issue, Did all those magic with configs and nothing..
Solution: run: php artisan route:clear
If issue remains same After cache clear or laravel routing rule, use 'composer dump-autoload'
If in your controller you have Model::findorFail($id) and an object with that ID does not exist, it can also lead to this error (in Laravel 6)