Laravel-表单发布错误的URL

时间:2016-03-09 12:20:22

标签: php laravel-5

我正在从零开始学习Laravel。

我正在做简单的表格" POST"但缺少一些东西,我无法找出遗漏的东西。

所以这是我的" routes.php":

Route::get('cards','CardsController@all'); Route::get('cards/{card}','CardsController@show'); Route::post('cards/{card}/notes','NotesController@new');

并查看:

<div>
                <form method="POST" action="cards/{{ $card->id }}/notes">
                    <div class="form-group">
                        <label>Note body:</label>
                            <textarea name="body" class="form-control"></textarea>
                    </div>

                    <button type="submit" class="btn btn-primary">Add</button>
                </form>
            </div>

这是控制器:

`        

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

  use App\Http\Requests;

   class NotesController extends Controller
   {
       public function new(Request $request){
          return $request->all();
      }
   }

`

现在,当我提交表单时,它会给我以下错误:

NotFoundHttpException in RouteCollection.php line 161

并且浏览器中的URL变为:

http://localhost:88/learning/cards/cards/1/notes

这绝对是错误的。

我想我错过了一些非常基本的东西。

请指导我。

谢谢。

2 个答案:

答案 0 :(得分:2)

将表单操作更改为,

package com.ggl.testing;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateRangeCalculator {

    private static final SimpleDateFormat format = new SimpleDateFormat(
            "dd MMM yyyy");

    public static void main(String[] args) {
        DateRangeCalculator dateRangeCalculator = new DateRangeCalculator();
        calculateDateInterval(dateRangeCalculator, "01 Jan 1987", "01 Jul 2013");
        calculateDateInterval(dateRangeCalculator, "01 Jul 2012", "01 Jul 2013");
        calculateDateInterval(dateRangeCalculator, "01 Jul 2012", "31 Jul 2012");
        calculateDateInterval(dateRangeCalculator, "01 Apr 2012", "31 Dec 2012");
    }

    private static void calculateDateInterval(
            DateRangeCalculator dateRangeCalculator, String startDateString,
            String endDateString) {
        try {
            Date startDate = format.parse(startDateString);
            Date endDate = format.parse(endDateString);
            DateInterval dateInterval = dateRangeCalculator.getDateInterval(
                    startDate, endDate);
            System.out.println("From " + format.format(startDate) + " to "
                    + format.format(endDate) + " is an interval of "
                    + dateInterval.getYears() + " years and "
                    + dateInterval.getDays() + " days.");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public DateInterval getDateInterval(Date startDate, Date endDate) {
        Calendar startCalendar = createCalendar(startDate);
        Calendar endCalendar = createCalendar(endDate);

        int years = -1;
        while (!startCalendar.after(endCalendar)) {
            startCalendar.add(Calendar.YEAR, 1);
            years++;
        }

        startCalendar.add(Calendar.YEAR, -1);

        int days = 0;
        while (!startCalendar.after(endCalendar)) {
            startCalendar.add(Calendar.DAY_OF_YEAR, 1);
            days++;
        }

        return new DateInterval(years, days);
    }

    private Calendar createCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public class DateInterval {
        private final int years;
        private final int days;

        public DateInterval(int years, int days) {
            this.years = years;
            this.days = days;
        }

        public int getYears() {
            return years;
        }

        public int getDays() {
            return days;
        }

    }

}

答案 1 :(得分:0)

您的行动始于&#34; cards /&#34;。将操作更改为以&#39; / cards /&#39;开头。这样它的路径就不会相对于当前路线计算出来。

<form method="POST" action="/cards/{{ $card->id }}/notes">