在laravel中将uri参数添加到路由名称

时间:2019-10-05 14:10:46

标签: php laravel routes crud

我正在研究Laravel项目,我制作了一个简单的CRUD系统,但是我有一个小问题

为了在我的项目中生成URL系统,我做了一个import pandas as pd import numpy as np import snowflake.connector OKTA_USER = 'username' OKTA_PASSWORD = 'pw!' con = snowflake.connector.connect( user=OKTA_USER, password=OKTA_PASSWORD, account='account', authenticator='account link', ) cur = con.cursor() cur.execute("USE WAREHOUSE WH_GENERAL") dataset = 'SELECT * FROM table LIMIT 10' 并将其添加到Route::macro

AppServiceProvider

到目前为止,这很好用,但是问题是我需要将Route::macro('crud', function () { Route::group([ ], function () { // Route::resource('', 'CrudController'); Route::get('{model}', 'CrudController@index'); Route::get('{model}/create', 'CrudController@create'); Route::post('{model}', 'CrudController@store'); /** << post **/ Route::get('{model}/{id}', 'CrudController@show'); Route::get('{model}/{id}/edit', 'CrudController@edit'); Route::match(['PUT', 'PATCH'],'{model}/{id}', 'CrudController@update'); /** << post **/ Route::delete('{model}/{id}', 'CrudController@destroy'); /** << post **/ }); }); 与它结合使用,并向其中添加->name()参数!

我要做的事的例子:

$model

有可能吗?,谢谢您

2 个答案:

答案 0 :(得分:1)

在此示例中,您可以循环一些数字并动态创建一些路线:

 for ($i = 0; $i < 5; $i++) {
     Route::get('test/' . $i, 'Controller@test_' . $i)->name('test.' . $i);
 }

您可以使用“ php artisan route:list”检查所有添加的路由。 我不建议您这样做,但是对于您而言,您可以在routes.php中的某处定义这样的数组,并在其上循环:

 $models = ['user', 'owner', 'admin'];
 foreach ($models as $model) {
     Route::get($model, 'CrudController@index')->name($model . '.index');
 }

或者您可以在配置中定义该数组(例如在“ config / app.php”中),例如:

 'models' => ['user', 'owner', 'admin'];

在routes.php中,您可以使用它来进行检索(不要忘记在更改app.php后运行“ php artisan config:cache”):

 $models = config('app.models');
 // foreach loop

答案 1 :(得分:0)

您可以将名称添加到模型的一组路由中
From the docs

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Route::macro('crud', function () {
            Route::name('{model}.')->group(function () {
                Route::get('{model}', 'CrudController@index')->name('index');
                Route::get('{model}/create', 'CrudController@create')->name('create');
                Route::post('{model}', 'CrudController@store')->name('store');
                Route::get('{model}/{id}', 'CrudController@show')->name('show');
                Route::get('{model}/{id}/edit', 'CrudController@edit')->name('edit');
                Route::match(['PUT', 'PATCH'], '{model}/{id}', 'CrudController@update')->name('update');
                Route::delete('{model}/{id}', 'CrudController@destroy')->name('destroy');
            });
        });
    }
}

Result

+-----------+----------------------------+-----------------+------------------------------------------------------------+
| Method    | URI                        | Name            | Action                                                     |
+-----------+----------------------------+-----------------+------------------------------------------------------------+
| GET|HEAD  | {model}                    | {model}.index   | App\Http\Controllers\CrudController@index                  |
| POST      | {model}                    | {model}.store   | App\Http\Controllers\CrudController@store                  |
| GET|HEAD  | {model}/create             | {model}.create  | App\Http\Controllers\CrudController@create                 |
| GET|HEAD  | {model}/{id}               | {model}.show    | App\Http\Controllers\CrudController@show                   |
| PUT|PATCH | {model}/{id}               | {model}.update  | App\Http\Controllers\CrudController@update                 |
| DELETE    | {model}/{id}               | {model}.destroy | App\Http\Controllers\CrudController@destroy                |
| GET|HEAD  | {model}/{id}/edit          | {model}.edit    | App\Http\Controllers\CrudController@edit                   |
+-----------+----------------------------+-----------------+------------------------------------------------------------+

现在您可以访问example.com/user/createexample.com/product/create,它们都采用相同的控制器方法

我希望这对您有帮助