dongyin2390 2015-12-01 05:07
浏览 115
已采纳

在Laravel内路由? 的.htaccess

I'm currently reading through the documentation and completing this quick-start guide: http://laravel.com/docs/5.1/quickstart#adding-tasks

I'm using Wamp and have installed this Laravel app in "www/laravel/quickstart5/" It works fine to access index through localhost/laravel/quickstart5/public/ When I submit a new task I get redirected to "localhost/task/"

This is my routes.php

<?php


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

/**
 * Display All Tasks
 */
Route::get('/', function () {
    $tasks = Task::orderBy('created_at', 'asc')->get();

    return view('tasks', [
        'tasks' => $tasks
    ]);
});
/**
 * Add A 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('/');

    // Create The Task...
});

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

    return redirect('/');
});

I have a file in /app/ called Task.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    //
}

I don't understand how this form is suppose to work. "<form action="/task" method="POST" class="form-horizontal">"

The only thing I get is that I guess I should change the .htaccess in the "/public"-folder

Current .htaccess

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>



RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

  • 写回答

2条回答 默认 最新

  • dsbruqxgt820011351 2015-12-01 05:33
    关注

    /task will always be relative to the root. You actually have a lot of options to "fix" this - none of which involve editing the .htaccess file.

    1. Use the url helper function. This will generate an absolute URL. In this case, it will be localhost/laravel/quickstart5/public/task.

      action="{{ url('task') }}"
      
    2. If you named your routes, you can use the route helper function instead to generate the absolute URL.

      action="{{ route('task') }}"
      
    3. Run php artisan serve from your favorite CLI. Then, you can access your Laravel project from http://localhost:8000.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面
  • ¥15 有没有人知道这种提示怎么关?现在不做ts项目了不知道咋关了,求解,现在我以前的js项目都是这种提示了
  • ¥15 为什么mysql做了碎片化处理data_free还是很高
  • ¥15 single positional indexer is out-of-bounds
  • ¥15 LSTM 模型数据量需要多少?
  • ¥15 mysql数据抓包开发报表
  • ¥15 linux系统下安装office打不开
  • ¥20 rsync脚本分发错误,文件不齐全
  • ¥15 labview顺序结构与时间延时