Laravel 5.3 路由数组

Laravel 5.3 Array on routes

我是 Laravel 的新手,自从我最初在 Java.

中编程以来,我仍在努力让自己熟悉它的语法

我在观看的其中一个教程中遇到了这种语法。

Route::get('/', [
    'uses'=>'ProductController@getIndex',
    'as' => 'product.index'
]);

我了解到 ProductController 是控制器 class,@getIndex 是驻留在 ProductController class 中的方法(如果您愿意的话)。

什么是usesasproduct.index我看到它们是键值对。

我可以将 usesas 修改为我想要的任何名称吗?

我在文件夹中的任何地方都没有看到 product.index。一开始以为是风景

这些是文件。

web.php

Route::get('/', [
    'uses'=>'ProductController@getIndex',
    'as' => 'product.index'
]);

ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class ProductController extends Controller
{
    public function getIndex(){

        return view('shop.index');
    }
}

Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = ['imagePath','title','description','price'];
}

请解释。

如果对此有任何有用的解释,我将不胜感激。

谢谢。

你说的对。该路由使用 ProductController 并请求 getIndex() 方法。是的,您可以随意命名路线,也可以随意命名您的方法。

作为别名,'as'是路由名称见here (Named Routes)

'product.index'

是路线名称。

所以你可以做...

Route::get('/', 'ProductController@getIndex')->name('product.index');

这将允许您使用此路由进行重定向。

return redirect()->route('product.index');

路由的命名是完全可选的。

希望对您有所帮助!