I'm using version 5.8 of laravel and I'm trying to initialize the routes. It is not possible to access the route created after the default route:
Route::get('/', function () {
return view('welcome');
});
I try to add a parameter in the second route:
Route::get('/page', ['as' => 'home', function (/page) {return view('page1') ;}]);
I got 404 error. How can I use multiple routes ?
This is my web.php file:
<?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('/page', function() { return view('page1'); })->name('home');
Route::get('/', function () {
return view('welcome');
});
Did you copied the second route, or what are you trying to do with this?
You can try this one instead as your syntax is wrong:
Route::get('/page', function() { return view('page1'); })->name('home');
-- EDIT
You can also return just the views from your routes if you don't plan to use a controller, like this:
Route::view('/', 'welcome');
Route::view('/page', 'page1');
Related
I have this error when a user returns from login to the admin page (i.e http://127.0.0.1:8000/admin), it should throw a 403 error if he/she doesn't sign in as the admin (i.e if he is not the admin)
Admin also experience this same error
Here's My Route
Laravel Version: 9.24.0
Please anyone should help
Here are my codes on 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('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::middleware(['auth','admin'])->name('admin.')->prefix('admin')->group(function() {
Route::get('/', [AdminController::class, 'index'])->name('index');
});
require __DIR__.'/auth.php';
in app\Providers\RouteServiceProvider.php
uncomment this line
protected $namespace = 'App\\Http\\Controllers';
then command do
php artisan optimize
// or
php artisan optimize:clear
Thank you everyone for your help
I have identified the problem, I didn't add this on my kernel
'admin' => \App\Http\Middleware\Admin::class
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'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 am attempting to navigate to a product page with laravel. The URL gets an id which is obtained from the database id which it uses to display the product.
Products controller file:
public function index()
{
$products = Product::all();
return view('products.index')->with('products',$products);
}
public function show($id)
{
$product = Product::find($id);
return view('products.show')->with('product',$product);
}
Error:
The requested URL /products/1 was not found on this server.
Here's my routes as requested:
<?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('HomePage');
});
Route::get('/about', function () {
return view('pages.about');
});
/*
* Login
*/
Route::get('/login', [
'as' => 'login',
'uses' => 'UserLoginController#showLogin',
]);
Route::post('/login', 'UserLoginController#postLogin');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('products','productcontroller');
You don't have any route like this /products/1 so need to add that route and assign the controller and action that you need . Eg.
Route::get('/product/{id}', 'ProductController#show')
Edit: is your controller class named correctly? I think issue is there. Yes you can use resource route for restful request response but route can't find your controller class. Name it correctly. ProductsController or something. Double check.
Can you paste your route? Problem is probably there. Not in your controller. Check the documentation, if you can't figure it out paste here.
the href tag was wrong and needed to be exactly what was in the localhost e.g. localhost/8888/public
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Laravel routes.php. So when I look at Auth examples for Laravel 5.2, I ALWAYS ALWAYS see the Route::auth(); and Route::get('/home', 'HomeController#index') encapsulated in { of the Route::group(['middleware' => ['web']], function () { thing. But whenever I use the commands php artisan make:auth all it does is create the one I showed above.
Any clues on why this does this? It works fine and all but I'm not 100% sure if it's functioning properly. But I can tell you that I can login and sign-up properly. Did they do any changes to Laravel?
the web middleware is now applied by default:
https://github.com/laravel/laravel/tree/master/app/Http
Also in Kernel.php
https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php