Laravel使用Controller @ show返回视图和api

时间:2016-04-13 01:08:47

标签: php laravel

我正在制作一个也将变成本机应用程序的Web应用程序,因此我想从一个路径显示json并返回另一个路径的视图。这是我在想但不确定如何在Laravel中编写if语句:

routes.php文件

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/notes', 'NotesController@index');
    Route::get('/notes/{note}', 'NotesController@show');
    Route::get('api/notes/{note}', 'NotesController@show');
});

NotesController.php

<?php

namespace App\Http\Controllers;

use App\Note;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NotesController extends Controller
{

    public function index() {

        $notes = Note::all();

        return view('notes.index', compact('notes'));
    }

    public function show(Note $note) {

        //if its from the general path show view
        return view('notes.show', compact('note'));

        //if its from the api path show json
        return $note;

    }

}

0 个答案:

没有答案