1

web.php

use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;

Route::get('/', PagesController::class, 'index');
Route::get('/test', PagesController::class, 'test');

PagesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function index()
    {
        return view('invoices.index');
    }

    public function test()
    {
        return view('invoices.test');
    }
}

Folder structure

views
  invoices
    index.blade.php
    test.blade.php
  layouts
    app.blade.php
  welcome.blade.php

Problem

When I run php artisan serve and go to localhost:8000 I see the welcome.blade.php template and when I go to localhost:8000/test I get 404 not found.

I tried the following commands with no success.

php artisan cache:clear
php artisan route:cache
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • because you made the `/test` route post request and you seem you want to access it with `get` request – Joseph Jan 07 '21 at 04:05
  • You have to use `get` method for `test` – A.A Noman Jan 07 '21 at 04:06
  • That was my bad, I tried to test it with post, but doesn't work, just like with get request. I edited the original code now. – Matej Zupan Jan 07 '21 at 04:07
  • And that also doesn't explain why / returns the welcome.blade.php since it should return the invoices.index.blade.php I tried to make a new project to see if there will be any difference, still the same. – Matej Zupan Jan 07 '21 at 04:08
  • it looks that there is another project opens in this port try to run your current project in another port like this `php artisan --port=8080` – Joseph Jan 07 '21 at 04:09
  • Just tried php artisan serve --port=8080 and get same result, even tried php artisan serve --port=8070 just to be sure without success. – Matej Zupan Jan 07 '21 at 04:14
  • could you rewrite your route defenition like this one `Route::get('/test', [PagesController::class, 'test']);` – Joseph Jan 07 '21 at 04:15
  • i suggest you to restart your pc then run `php artisan serve` then check it – Kamlesh Paul Jan 07 '21 at 04:26
  • I tried that as well, can something still run after PC restart? Also, in previous project I had storage and I just see that on fresh install, I now have storage folder as well, but I don't think that should be there on fresh installation right? – Matej Zupan Jan 07 '21 at 04:29

3 Answers3

0

Check your route definition. It should be something like:

Route::get('/', 'PagesController@index');

or

Route::get('/', [PagesController::class, 'index']);

You can also try:

Route::view('/', 'invoices.index');
P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
0

You can try this

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;

Route::get('/', [PagesController::class, 'index']);
Route::get('/test', [PagesController::class, 'test']);

Reference

VIKAS KATARIYA
  • 5,867
  • 3
  • 17
  • 34
  • Yeah, I tried that since someone else suggested that syntax as well, still the same problem. Might be something wrong with my installation since I never used linux before, but I followed step by step ubuntu and laravel installation and it worked for that person. – Matej Zupan Jan 07 '21 at 04:23
0

Laravel 8 Route structure change

Route::get('/', [PagesController::class, 'index']);

If you want to write previous structure, just add route namespace in Providers -> RouteServiceProvider.php

Route::middleware('web')
        ->namespace($this->namespace)        // make sure this is present in your code
        ->group(base_path('routes/web.php'));

I hope it will be work!