warpig's avatar
Level 12

Route without controllers

How could I define a route without the use of any controllers, possibly in 1 line? I've tried several ways like using

Route::view('/hello', 'hello');

or

Route::view('/hello', 'hello', function () {return view('pages.hello'); });

Documentation says I may use the view helper in my routes, 1st parameter should be the URI and second the name of it, but I don't understand as I can't make it work, am not returning any files so I get a 404.

0 likes
15 replies
tykus's avatar

This is how you would use a Closure

Route::get('/hello', fn () => view('pages.hello'));
//or 
Route::get('/hello', function () {
    return view('pages.hello')
});

But, this should also work:

Route::view('/hello', 'pages.hello');
2 likes
Snapey's avatar

is your view file called hello.blade.php or is it in a folder????

this works. Route::view('/hello', 'hello'); provided your hello blade is in the root of the views folder AND there isn't a route in the file before it that captures 'hello' as a parameter

2 likes
warpig's avatar
Level 12

@Snapey nah, but i am doing some exercises for a "roadmap" and there are like 12 tasks to do, i have been stuck on this basic routing for probably an hour!!! the route for this view is actually "resources.views.pages.about"

warpig's avatar
Level 12

@tykus well

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('About') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    This project is for testing your Laravel routing skills.
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
tykus's avatar

@warpig so nothing that might become a 404 response; anything in the AppLayout?

What URL are you visiting; is this Route nested?

warpig's avatar
Level 12

@tykus not to my knowledge, http://127.0.0.1:8000/about this blade doesn't have anything but those blade components of slot and header, this was all that was given:

 // Task 3: point the GET URL "/about" to the view
// resources/views/pages/about.blade.php - without any controller
// Also, assign the route name "about"
// Put one code line here below
Route::view('/about', 'pages.about')->name('about'); <!-- my input.....
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">

        <title>{{ config('app.name', 'Laravel') }}</title>

        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

        <!-- Styles -->
        <link rel="stylesheet" href="{{ asset('css/app.css') }}">

        <!-- Scripts -->
        <script src="{{ asset('js/app.js') }}" defer></script>
    </head>
    <body class="font-sans antialiased">
        <div class="min-h-screen bg-gray-100">
            @include('layouts.navigation')

            <!-- Page Heading -->
            <header class="bg-white shadow">
                <div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
                    {{ $header }}
                </div>
            </header>

            <!-- Page Content -->
            <main>
                {{ $slot }}
            </main>
        </div>
    </body>
</html>
tykus's avatar

@warpig this? https://github.com/LaravelDaily/Test-Laravel-Routes

Are you completing all of the tasks before running the tests? Looks like there a navigation links that require a dashboard route but that task comes later...

You can easily debug this stuff yourself - it is an essential skill for development - just dump() the TestResponse in the Test Example.

1 like
warpig's avatar
Level 12

@tykus yep!! that's the one -_- well I am not sure about any tests, did I missed reading the instructions ? since that route comes later .... should i register then ??

warpig's avatar
Level 12

@tykus when you have something like this and then run php artisan test ..... where is the dump at??? because i can't see it

    public function test_about_page_is_loaded()
    {
        $response = $this->get('/about');
        $response->assertViewIs('pages.about');
        $response->dump();
    }
tykus's avatar

@warpig you can dump the contents of the response like this:

public function test_about_page_is_loaded()
{
    $response = $this->get('/about')->dump();
    $response->assertViewIs('pages.about');
}

Read through the dumped HTML to find the reason for the failure.

Snapey's avatar

do you have your site hosted correctly or do you see /public in your url?

if so /about will be pointing to the wrong place

1 like

Please or to participate in this conversation.