Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Slillz's avatar

Redirect to another view

I'm using Laravel 5.2. I have a simple form on the homepage. When a user clicks search on that form it should send them to a registration page. It's just a dummy form so no data is being submitted. I'm not sure what I need to put in my routes file. Do I need to put something in my controllers file as well? I have already created the register blade. Any help will be appreciated.

This is what I have on my main page...

form action="{{route('register')}}" method="post"

0 likes
30 replies
zachleigh's avatar

Why not just use an anchor tag?

<a href="{{route('register')}}"></a>
Slillz's avatar

Ok. I changed it but when I click on the button it's still not able to find that page. Is the following all I need in my routes...?

return Redirect::route('register ');

Slillz's avatar

Even with an anchor tab it's still not going to the right view.

Slillz's avatar

@zachleigh I even tried to do it with the anchor that you mentioned but I keeping the following error message. The welcome and register view are in the same folder. "Not Found

The requested URL /register was not found on this server."

The following is my routes.php...

Route::get('/', function () { return view('welcome'); });

Route::get('register', function () { return view('register'); })->name('register');

zachleigh's avatar

Why not just do this?

<a href="/register"></a>

And then do the simplest thing possible in routes.

Route::get('register', function () {
    return view('register'); 
});
Slillz's avatar

@jlrdw So I tried do it as a redirect but to no avail.

I changed my route to..

Route::get('register', function () { return redirect('register'); })->name('register');

jlrdw's avatar

Besides what @zachleigh said, are you on an up-to-date version? There was some major changes in web middleware.

jimmck's avatar

Is the client sending a GET or a POST? The route expects a GET. Your example is a POST...

Slillz's avatar

@zachleigh I did it exactly as you said and that doesn't work as well.

@jlrdw I just installed it this week.

Should my routes be under the middleware function. This is how it looks fully.

Route::get('/', function () { return view('welcome'); });

Route::get('register', function () { return view('register'); })->name('register');

Route::group(['middleware' => ['web']], function(){

});

zachleigh's avatar

Have you tried other routes? Do they work? Does you htaccess file look like this?

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    Options +FollowSymLinks
    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Slillz's avatar

I don't have that file. This is all being ran on a Ubuntu distro. The welcome route works jut fine. If I make that that register route the homepage it works fine as well.

spekkionu's avatar

You don't have a .htaccess file located at public/.htaccess?
Laravel comes with one so it was either deleted or not uploaded when you installed the app of your server.
Create the file with content from the laravel repo.
https://github.com/laravel/laravel/blob/master/public/.htaccess

You will also need to make sure your vhost has AllowOverride All for the public directory.

spekkionu's avatar

I just added that file

Are you able to access routes other than the homepage now?

Slillz's avatar

It's still not finding my register page.

spekkionu's avatar

Can you put an exit('test') at the top of public/index.php (but after the <?php) and visit your register page and tell me if you see test on the page.

That will tell us if we need to be looking at your php code or your server configuration.

spekkionu's avatar
Level 47

The welcome view is my homepage. Do you want me to put that code there?

No, put it in public/index.php which is the frontcrontroller that all requests that aren't static files should be running.
The file should look like the following https://github.com/laravel/laravel/blob/v5.2.29/public/index.php If you don't see test then the apache rewrite rules aren't sending requests to this script which would cause only the welcome route to show.

Slillz's avatar

I added exit('test') to index.php and when I open register.blade.php I don't see exit in the editor.

Slillz's avatar

@spekkionu For some reason I can't get these views to open in a browser just by opening the register.blade.php file.

With exit('test') in the index.php I'm no longer able to open up the homepage though.

Edit: I was finally able to open that file, but I don't see exit being displayed in the window. Only the content of the view. Also I changed the .conf file to AllowOverride All

spekkionu's avatar

When I said to open the register route in the browser I did not mean the view file. I meant the url the route is pointing to which looks like /register.

What you see when you open up the homepage should be what you see for every url.
Because you don't that means the problem is likely your server configuration and has nothing to do with your routes, controllers, or views.
You can remove the exit statement you added as that was just a test.

Try to make sure your document root is pointed at your public directory and not the root of the application.
Because you said you were able to open your view file in the browser and the views directory is outside the public directory you likely have this pointed at the wrong place.

Slillz's avatar

@spekkionu The problem is solved. After running "sudo a2enmod rewrite" and restarting the apache server the routes are now found. I don't understand what that command does but I will google it. Thanks for all of your help.

Please or to participate in this conversation.