menu

Questions & Answers

How do i make a Laravel web.php route work on a new project?

sorry for my english. I'm trying to create a laravel route but i just can't make it work. My project name is "portalRAG". It's a web app. When i access "my.address/PortalRAG" it works just fine, but i can't make any other route work.

This is a new Laravel Project. It's almost empty and i haven't touched any major configuration other than creating some 1 or 2 views,controllers and model and only created some html code. Here's my web.php file:

  <?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers;
use App\Http\Controllers\ragController\ragHomeController;
/*
|--------------------------------------------------------------------------
| 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('login');
});

/* NOT WORKING
Route::get('test', function () {
    return view('login');
});
 */

 Route::get('test','App\Http\Controllers\ragController\ragHomeController')->name('test');

I simply want to access "test" route. The controller i'm trying to use it's called ragHomeController and it's inside a ragController (a folder inside the basic Controller file).

Here's ragHomeController.

<?php

namespace App\Http\Controllers\ragController;

use App\Http\Controllers\Controller;
use App\Models\ragModel\ragHomeModel;
use Illuminate\Http\Request;

class ragHomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       echo("WHATEVER");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\ragModel\ragHomeModel  $ragHomeModel
     * @return \Illuminate\Http\Response
     */
    public function show(ragHomeModel $ragHomeModel)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\ragModel\ragHomeModel  $ragHomeModel
     * @return \Illuminate\Http\Response
     */
    public function edit(ragHomeModel $ragHomeModel)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\ragModel\ragHomeModel  $ragHomeModel
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ragHomeModel $ragHomeModel)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\ragModel\ragHomeModel  $ragHomeModel
     * @return \Illuminate\Http\Response
     */
    public function destroy(ragHomeModel $ragHomeModel)
    {
        //
    }
    public function __invoke()
    {
    }
}

What i'm getting wront? i have tried clearing cache, clearing route cache, and nothing works. How should i access my "test" route? (I have tried every way and i still can't make it work). "my.address/PortalRAG/test"? "my.address/test"?

Comments:
2023-01-24 23:01:10
What happens when you try to access the test route?
2023-01-24 23:01:10
@aynber "404 NOT FOUND" no matter what.
2023-01-24 23:01:10
instead of 'App\Http\Controllers\ragController\ragHomeController', try ragHomeController::class. Does the route appear when you run php artisan r:l ?
2023-01-24 23:01:10
I think your issue is not how you are declaring the route, but that you have a misconfiguration on your web server. If your route was not successfuly registered, then you would get an exception, but you are getting a 404
2023-01-24 23:01:10
Ideally your project would have its own vhost and the url would be PortalRAG.local (or something). If you're inside your PortalRAG folder and run php artisan serve you can access the project via localhost:8000 (I think) and your route via localhost:8000/test
2023-01-24 23:01:10
@matiaslauriti Yeah the thing is, in the same server, i have multiple laravel projects and they all work 100% fine... I think it may be a Laravel config problem, maybe something i need to do on RouteServiceProvider or something?
2023-01-24 23:01:10
I have never defined a laravel project inside a folder like you. I am not sure if you have to specify APP_URL=https://my.domain/PortalRAG, how do you have it defined? also, if you run php artisan route:list, what do you get back?
2023-01-24 23:01:10
@matiaslauriti Bro at this point i'm start to think it may be a Laravel bug. I searched all over internet and nothing works. I even created another controller outside of the folder, and it still not working.
Answers(2) :

please write you route like below

 Route::get('test',[ragHomeController::class,'Your_Method_Name'])->name('test');

and import your controller in top of your web

use App\Http\Controllers\ragHomeController;

Note:sometime it won't work so you have to run

php artisan optimize

if you are using xampp to run your laravel project then you can access your route like below

"my.address/PortalRAG/public/test"
Comments:
2023-01-24 23:01:10
Thanks for the answer. I have done all the steps and it's still "NOT FOUND". Think i'll go crazy lol. Should i acces it using "my.address/test",right?
2023-01-24 23:01:10
are you using xampp or you are runing PHP artisan serve?
2023-01-24 23:01:10
Artisan server.

you didn't use the method that located in ragHomeController so write it in this way

Route::get('test',[App\Http\Controllers\ragController\ragHomeController::class, 'index'])->name('test');

now open your route like this:

my.address/PortalRAG/test