This is actually works! Eventhough I don't really understand why that happens?
web.php
Route::resource('cpages/ads/{type}', 'AdsController')->parameters([
'{type}' => 'optional?'
]);
If I changed it to this : Route::post('cpages/ads/{type}', 'AdsController');
It gives me this error message:
UnexpectedValueException
Invalid route action: [App\Http\Controllers\AdsController].
https://localhost/axe-backend/public/cpages/ads/slider
Hide solutions
`App\Http\Controllers\AdsController` is not invokable.
The controller class App\Http\Controllers\AdsController is not invokable. Did you forget to add the __invoke method or is the controller's method missing in your routes file?
ref: https://stackoverflow.com/questions/57501421/laravel-not-picking-up-invoke-method
Why I cannot use resources?
AdsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Advertisement;
use Illuminate\Support\Str;
use Session;
class AdsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($type)
{
//
return view('admin.advertisement.ads', compact('type'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, $type)
{
//
$this->validate($request, [
'ad_title' => 'required',
'ad_caption' => 'required',
'ad_url' => 'required',
'images' => 'required'
]);
if($type == "slider") {
$dir_location = 'slider';
$dir = 'images/slides/';
}elseif($type == "featured_index") {
$dir_location = 'featured_index';
$dir = 'images/featured_index/';
}elseif($type == "catalog_ads") {
$dir_location = 'catalog_ads';
$dir = 'images/catalog_ads/';
}elseif($type == "banner_ads") {
$dir_location = 'banner_ads';
$dir = 'images/banner_ads/';
}
$ads = new Advertisement;
$ads->ad_type = $type;
$ads->ad_title = $request->ad_title;
$ads->ad_caption = $request->ad_caption;
$ads->ad_url = $request->ad_url;
// Mengelola nama
$extension = $request->file('images')->extension();
$name_wo_extension = pathinfo($request->file('images')->getClientOriginalName(), PATHINFO_FILENAME);
$slug = Str::slug($name_wo_extension, '-');
$name = $slug . '.' . $extension;
// check the existance of old file
$count = count(Advertisement::where('ad_img_url', $dir.$name)->get());
while($count != 0)
{
$slug = $slug . '-copy';
}
$ads->ad_img_url = $dir . $slug;
$request->file('images')->storeAs($name, $dir_location);
$ads->save();
Session::flash('flash', 'Ads type :'. $type . ' created');
return redirect()->back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
I am afraid if I am going to need these three: index, store, & destroy from AdsController