2
votes

Edit: I think this problem is due to not setting up apache properly, these two links helped me.

Solution:

http://laravel.io/forum/06-11-2014-not-found-but-route-exists

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts

End Edit

I'm following the Laravel 5.2 quickstart guide. https://laravel.com/docs/5.2/quickstart

I'm running ubuntu 14.04.2

The website runs initially, but then when I click on the add Task button I run into a 404 Not Found error.

As per the guide I run the following commands to setup the complete quickstart project.

git clone https://github.com/laravel/quickstart-basic quickstart
cd quickstart
composer install
php artisan migrate

Additionally I run the following commands because I'm on an ubuntu server:

sudo chown -R www-data.www-data quickstart
sudo chmod -R 755 quickstart
sudo chmod 777 quickstart/storage

The routes.php file looks like:

<?php
use App\Task;
use Illuminate\Http\Request;

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

        $tasks = Task::orderBy('created_at', 'asc')->get();

        return view('tasks', [
            'tasks' => $tasks
        ]);
    });

    Route::post('/task', function (Request $request) {

        $validator = Validator::make($request->all(), [
            'name' => 'required|max:255',
        ]);

        if($validator->fails()) {
 return redirect('/')
                ->withInput()
                ->withErrors($validator);
        }

        $task = new Task;
        $task->name = $request->name;
        $task->save();

        return redirect('/');
    });
        //
    Route::delete('/task/{task}', function (Task $task) {
    $task->delete();

    return redirect('/');

    });

});

I tried changing some code in the resources/view/tasks.blade.php from:

 <!-- New Task Form -->
                    <form action="/task" method="POST" class="form-horizontal">

To the location of the actual public folder of the laravel project :

 <!-- New Task Form -->
                    <form action="/learninglaravel/laraquickstart/quickstart/public/task" method="POST" class="form-horizontal">

So my problem is only when I press the button I get a 404 Error

//***** Edit:

I have tried having editing the post route but that doesn't fix the problem.

Route::post('/learninglaravel/laraquickstart/quickstart/public/task', function (Request $request) {

            $validator = Validator::make($request->all(), [
                'name' => 'required|max:255',
            ]);

            if($validator->fails()) {
     return redirect('/')
                    ->withInput()
                    ->withErrors($validator);
            }

            $task = new Task;
            $task->name = $request->name;
            $task->save();

            return redirect('/');
        });

The error is:

Not Found

The requested URL /learninglaravel/laraquickstart/quickstart/public/task was not found on this server.

Instead of:

Not Found

The requested URL /task was not found on this server.

I did what you suggested James and you were right, it makes the URL change to

/learninglaravel/laraquickstart/quickstart/public/task

but I still get the error of page not found. The only difference is the error message changed from the bellow error to the above. (I also tried manually changing both the url in the route.php file and the tasks.blade.php to reflect the same url. So the above URL would be both in the routes.php and the tasks.blade.php

I'm running on an ubuntu AWS server maybe I made a mistake on that end? I think the quickstart tutorial was written for homestead as I ran into problems with the database.(I just had to manually create one. The guide never specified that. I think it might be pre-configured in homestead to automatically use a database)

Cheers

3
So you are just trying to post the data from the form to a URL like localhost/quickstart/public/task, is that correct? - James
its on an ubuntu server on aws, sorry shoulda said aws, so it'll be an ip address like 52.60.22.45/learninglaravel/laraquickstart/quickstart/public - Mr Gibbous

3 Answers

2
votes

From the guide on Quickstart, you can generate your form action URLs using the url helper, which:

generates a fully qualified URL to the given path.

As such you can use it like so on your form:

<!-- New Task Form -->           
<form action="{{ url('task') }}" method="POST" class="form-horizontal">

This will ensure you are always posting to the correct URL for your domain, without you having to specify this yourself manually.

Give it a go and see what I mean, but in your case it would generate a url for you to your task route along the lines of this (based off your comment); 52.60.22.45/learninglaravel/laraquickstart/quickstart/public/task

1
votes

When you change your form action parameter to

/learninglaravel/laraquickstart/quickstart/public/task

You are going to make HTTP POST request to

{scheme}://{domain}//learninglaravel/laraquickstart/quickstart/public/task

So laravel is going to look for this route and obviously it won't find it.

0
votes

Just enabled mod_rewrite engine and it will work perfectly. I have spend more then 2 hrs to find the solution and the solution. So don't wast your time and just enable the mod_rewrite engine by following command and try again:

sudo a2enmod rewrite

And dont forget to restart the apache server

sudo /etc/init.d/apache2 restart

or

sudo service apache2 restart