can we build a dynamic url in laravel, which can include the blade file dynamicly
Iam trying to build an easy method to build a laravel
project. I got from articles that we should build controller
and blade
file
for example this command php artisan make:livewire Admin.CourseList
if we had a lot of page it's going to be lot of file,
so i hava a questions can we just build one controller to use in a lot of blade
file, the idea is making dynamic url
, then the url
detected in controller
, controller
get data from url
to fetch the blade
file.
Iam trying to use this method
Route::get('master/{fileis}', Master::class); ( my route )
<?php
// master controller
namespace App\Http\Livewire\Admin;
use Livewire\Component;
class Master extends Component
{
public function index ($field)
{
return view('livewire.admin.$filed');
}
}
I wish that when url
with $field
are going to be the file name.
So the effect i wish for example mywebsite.com/master/login-file
, this url going to fetch login-file.blade.
but it's didn't work.
Solution 1:
There a many typo in your question, but i got what you mean.
Try This.
// in route
Route::get('/{$field}', [Master::class, 'index']);
// master index()
public function index ($field)
{
// `double quote` for `var` inside `string`
return view("livewire.admin.$field");
}
Remember to create necessary view
or add if
with logical
View::exists('your_view')
in your view
call
EDIT: in LARAVEL LIVEWIRE
I'll give you some picture.
run php artisan make:livewire admin.course
// in route
Route::livewire('/course/{course}', 'admin.course');
// in Course.php
class Course extends Component
{
public $course;
public function mount($course)
{
// init course in mount cycle
$this->course = $course;
}
public function render()
{
// insert `public $coursea` to your `view`
return view("livewire.admin.$this->course", [
'course' => $this->course,
]);
}
}