Created
May 7, 2019 10:26
-
-
Save 5lineofcode/a57fd548ba8e62b784194b397339402f to your computer and use it in GitHub Desktop.
Router Dasar Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
use Illuminate\Support\Facades\Input; | |
use Illuminate\Support\Facades\DB; | |
use Carbon\Carbon; | |
use Illuminate\Http\Request; | |
Route::get('/', function () { | |
return Carbon::now()->add(10, 'day');; | |
}); | |
Route::get('/product/{product_id}',function($product_id){ | |
$products = DB::table("product") | |
->where("product_id",$product_id) | |
->where("product_id","=",$product_id) | |
->get(); | |
return json_encode($products); | |
}); | |
Route::get('/product',function(){ | |
$products = DB::table("product")->get(); | |
return json_encode($products); | |
}); | |
Route::post('/product',function(){ | |
$product_name = Input::get("product_name"); | |
$price = Input::get("price"); | |
$stock = Input::get("stock"); | |
$response = DB::table("product")->insert([ | |
"product_name" => $product_name, | |
"price" => $price, | |
"stock" => $stock, | |
]); | |
return [ | |
"responses" => $response, | |
"message" => "Success" | |
]; | |
}); | |
Route::put('/product/{product_id}',function($product_id){ | |
$product_name = Input::get("product_name"); | |
$price = Input::get("price"); | |
$stock = Input::get("stock"); | |
$response = DB::table("product") | |
->where("product_id",$product_id) | |
->update([ | |
"product_name" => $product_name, | |
"price" => $price, | |
"stock" => $stock, | |
]); | |
return [ | |
"responses" => $response, | |
"message" => "Success" | |
]; | |
}); | |
Route::delete("/product/{product_id}",function($product_id){ | |
$response = DB::table("product") | |
->where("product_id",$product_id) | |
->delete(); | |
return [ | |
"responses" => $response, | |
"message" => "Success" | |
]; | |
}); | |
Route::post("/photo-upload",function(Request $request){ | |
$photo = $request->file('photo'); | |
$destinationPath = 'uploads'; | |
$filename = uniqid() ."-" . $photo->getClientOriginalName(); | |
$upload_path = $destinationPath . "/" . $filename; | |
$photo->move($destinationPath,$filename); | |
return json_encode([ | |
"success" => true, | |
"photo_location"=> $upload_path | |
]); | |
}); | |
Route::post('/order/add/item/{order_id}',function($order_id){ | |
DB::transaction(function () { | |
DB::table("product") | |
->where("product_id",$product_id) | |
->delete(); | |
}); | |
DB::beginTransaction(); | |
addOrder(); | |
addCustomer(); | |
DB::commit(); | |
}); | |
Route::prefix('admin')->group(function () { | |
Route::get('/{product_id}', function($product_id){ | |
}); | |
Route::get('/', function () { | |
return "Hello Admin"; | |
}); | |
Route::post('/', function(){ | |
return "Post from Admin"; | |
}); | |
Route::put('/{product_id}', function($product_id){ | |
}); | |
Route::delete('/{product_id}', function($product_id){ | |
}); | |
}); | |
Route::get("/project",function(){ | |
if(Session::get("logged_in")==true){ | |
return "Hello Project"; | |
} | |
else { | |
return [ | |
"errorCode" => 401, | |
"message" => "Unautohirzed Access", | |
]; | |
} | |
}); | |
Route::post("/login",function(){ | |
$username = Input::get("username"); | |
$password = Input::get("password"); | |
if($username=="admin" && $password=="123456"){ | |
Session::put("logged_in",true); | |
return [ | |
"success" => true, | |
"message" => "Login Success!" | |
]; | |
} | |
return [ | |
"success" => false, | |
"message" => "Username atau password salah!" | |
]; | |
}); | |
Route::get('/logout',function(){ | |
Session::flush(); | |
return [ | |
"success" => true, | |
"message" => "Berhasil Logout!" | |
]; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment