larashopping / shopping
laravel扩展包中的基本购物车系统
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-18 20:33:31 UTC
README
设置权限
- 设置权限: chmod -R o+w storage bootstrap
设置自己的包
-
在主根目录下创建名为 packages 的文件夹
-
添加 vendor 名称的文件夹
-
添加项目名称的文件夹
-
在项目名称下添加 src 文件夹
-
您的文档应为 packages/laravelcms/shopping/src
-
在您的终端路径 shopping 中
-
命令: composer init 按回车
-
输入包路径 packages/laravelcms 并按回车
-
在 minimum-stability: dev 然后按回车直到退出
-
在您的主 composer.json 中添加
-
"autoload": { "classmap": [ "database" ], "psr-4": { "App": "app/", "laravelcms\shopping": "packages/laravelcms/shopping/src/" } },
-
compososer dump-autoload
-
在您的 src/ShoppingServiceProvider.php 中添加服务提供者
namespace laravelcms\shopping; use Illuminate\Support\ServiceProvider; class ShoppingServiceProvider extends ServiceProvider { public function boot() { include DIR.'/routes.php'; //For publich and copy this view in your another laravel project command:php artisan vendor:publish $this->publishes([ DIR.'/views' => base_path('resources/views/laravelcms/shopping'), ]);
$this->publishes([ __DIR__ . '/migrations' => $this->app->databasePath() . '/migrations' ], 'migrations'); $this->publishes([ __DIR__ . '/seeds' => $this->app->databasePath() . '/seeds' ], 'seeds');}
public function register() { // register our controller $this->app->make('laravelcms\shopping\ProductController'); $this->loadViewsFrom(DIR.'/views', 'add-product');
$this->loadViewsFrom(DIR.'/views', 'manage-product');
}
} -
在您的 src/ProductController.php 中添加控制器
namespace laravelcms\shopping; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; use laravelcms\shopping\models\Item; error_reporting(0); class ProductController extends Controller { public function form_product() {
return view('add-product::add_product'); } public function form_submitted(Request $request) { $product_name = stripslashes($request->product_name); $product_amount = stripslashes($request->product_amount); $product_desc = stripslashes($request->product_desc); $date = date("Y-m-d h:i:s"); $stm=new Item; $stm->product_name=$product_name; $stm->product_amount=$product_amount; $stm->product_desc=$product_desc; $stm->created_at=$date; $stm->updated_at=$date; $stm->save(); //session()->flash('sussess', 'Successfully updated invoice price'); return redirect("manage-product"); } public function list_product() { $id=isset($_REQUEST["id"])?$_REQUEST["id"]:0; if($id>0) { Item::where("id",$id)->delete(); } $row=Item::all(); return view('manage-product::manage_product')->with("row",$row); }}
-
在 src/routes.php 中添加路由 Route::get("test","laravelcms\shopping\ProductController@index"); Route::get("add-product","laravelcms\shopping\ProductController@form_product"); Route::get("manage-product","laravelcms\shopping\ProductController@list_product"); Route::post("add-products","laravelcms\shopping\ProductController@form_submitted");
-
添加 views 文件夹并在其中添加文件,应为 src/views/add_product.blade.php src/views/manage_product.blade.php
-
再次运行: composer dump-autoload
-
在 src/migrations/2017_03_14_000000_create_product_demo_items_table.php 中添加迁移文件夹 use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreateProductDemoItemsTable extends Migration { public function up() { Schema::create('laracms_product', function(Blueprint $t) { $t->increments('id')->unsigned(); $t->string('product_name', 200); $t->string('product_amount', 10); $t->string('product_desc', 500); $t->timestamps(); }); }
public function down() { Schema::drop('laracms_product'); }}
-
添加 Models 文件夹 src/models/Items.php namespace laravelcms\shopping\models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model { protected $table = 'laracms_product'; }
-
==============如果想要重用您的代码
-
运行: php artisan vendor:publish
-
它将复制您所有包文件到主 Laravel 项目
-
运行: php artisan migrate 它将在您的数据库中创建产品表,您可以使用它
-
您可以使用 add-product 和 manage-product 检查