我是Laravel的新手,因为我最初是用Java编程的,所以我仍然试图让自己熟悉它的语法。
我在看的一个教程中遇到了这个语法。
Route::get('/', [
'uses'=>'ProductController@getIndex',
'as' => 'product.index'
]);
我理解ProductController
是控制器类,@getIndex
是驻留在ProductController
类中的方法(如果您愿意)。
什么是uses
, as
和product.index
我看到它们是成对的键和值。
我可以修改uses
和as
到我想要的任何名称吗?
我没有看到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 '是路由名,见这里(Named Routes)。
'product.index'
为路由名。
所以你可以做…
Route::get('/', 'ProductController@getIndex')->name('product.index');
这将允许您使用此路由进行重定向。
return redirect()->route('product.index');
路由的命名是完全可选的。
希望有帮助!