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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵