2
votes

I am using Laravel 5 and started the quickstart from the Laravel website. If i used the post function that is in the code it redirects to localhost/LOCATION instead of localhost/PROJECT/public/LOCATION.

It might be something in my config but i can't figure out what it is.

here is the code from my router it appears to be a problem in the post task function.

<?php

use App\Task;
use Illuminate\Http\Request;

Route::group(['middleware' => ['web']], function () {
    /**
     * Show Task Dashboard
     */
    Route::get('/', function () {
        return view('tasks', [
            'tasks' => Task::orderBy('created_at', 'asc')->get()
        ]);
    });

    /**
     * Add New Task
     */
    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('/');
    });

    /**
     * Delete Task
     */
    Route::delete('/task/{id}', function ($id) {
        Task::findOrFail($id)->delete();

        return redirect('/');
    });
});

With this code I post to the route

<form action="/task" method="POST" class="form-horizontal">
    {{ csrf_field() }}

    <!-- Task Name -->
    <div class="form-group">
        <label for="task-name" class="col-sm-3 control-label">Task</label>

        <div class="col-sm-6">
            <input type="text" name="name" id="task-name" class="form-control" value="{{ old('task') }}">
        </div>
    </div>

    <!-- Add Task Button -->
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-6">
            <button type="submit" class="btn btn-default">
                <i class="fa fa-btn fa-plus"></i>Add Task
            </button>
        </div>
    </div>
</form>

UPDATE If I change my form action to "/quickstart/public/task" it works but /task should redirect to it automatically in laravel if i remember correctly

3
How are you using the post function, in a form? If so, please post the code for the formhaakym
Yeah from a form ill put it in there thxNico Shultz

3 Answers

2
votes

you can use laravel helper function.For more https://laravel.com/docs/5.2/helpers#urls

My suggestion is for you use url() like

<form action = "{{ url('task') }}">
2
votes

You can name your route (look at https://laravel.com/docs/5.2/routing#named-routes)

and call it in your form action

<form action = "{{ route('name_of_my_route') }}"
0
votes

You have wrong web server configuration. You should set up public directory as root directory.

localhost/PROJECT/public/LOCATION is a wrong address, localhost/LOCATION is correct address.

For example, if you're using Apache:

<VirtualHost *:80>
  ServerName myapp.localhost.com
  DocumentRoot "/home/vagrant/projects/myapp/public"
  <Directory "/home/vagrant/projects/myapp/public">
    AllowOverride all
  </Directory>
</VirtualHost>