Laravel 5.7 routing pass to controller just the second parameter - laravel

I have the following route
Route::get('/{slug}/pd/{public_id}', 'Products\ShowController');
And I want to pass to ShowController just the public_id parameter.
class ShowController extends Controller
{
public function __invoke($public_id)
{
dd($public_id);
}
}
If I run the code above it returns the slug value. I need slug to be just a wildcard in url.

If the slug is certain words in your db, probably you can check route prefix to remove slug from the route. If not, Just ignore the slug after getting it in controller. If its there in route it will be available in controller.
class ShowController extends Controller
{
public function __invoke($slug, $public_id)
{
dd($public_id);
}
}

Related

laravel resource url depend on model?

i am run this command for model, migration, resource controller. php artisan make:model QuestionAnswer -mc -r ..
Resource Route
Route::resource('faq','QuestionAnswerController');
My Edit Function
public function edit(QuestionAnswer $questionAnswer)
{
// return $questionAnswer;
return view('backend.faq.edit',get_defined_vars());
}
Edit Route
{{route('admin.faq.edit',$questionAnswer->id)}}
Edit function return $questionAnswer return null
Below Picture
When i Change resource route like model name
Route::resource('question-answer','QuestionAnswerController');
edit function return $questionAnswer return object mean expected output ..
Picture
Question
laravel resource url depend on model or something ?
if i am wrong somewhere for Route::resource('faq','QuestionAnswerController'); please comment i will remove my question..
Bacause your route parameter is question_answer, so change the controller to :
public function edit(QuestionAnswer $question_answer)
{
dd($question_answer);
}
Alternatively, you can specifically tell the resource what the route parameter should be named :
Route::resource('faq','QuestionAnswerController')
->parameters(['faq' => 'questionAnswer']);
Now you can access $questionAnswer as parameter :
public function edit(QuestionAnswer $questionAnswer)
{
dd($questionAnswer);
}
The official documentation of Naming Resource Route Parameters will be found here
Laravel resource generate prams base on url example
Route::resource('faq','QuestionAnswerController');
// this will generate url like
Route::get('faq','QuestionAnswerController#index')->name('faq,index');
Route::post('faq','QuestionAnswerController#store')->name('faq,store');
Route::get('faq/{faq}','QuestionAnswerController#show')->name('faq,show');
Route::put('faq/{faq}','QuestionAnswerController#update')->name('faq,update');
Route::delete('faq/{faq}','QuestionAnswerController#destroy')->name('faq,destroy');
so here in controller you need to accept that faq like this
public function edit(QuestionAnswer $faq) // here $faq should match with route prams
{
return $faq;
}
or you can change route url faq to questionAnswer then your old code will work
ref link https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller

Multiple implicit model binding with one controller in Laravel

I am trying to attach multiple models with one controller using implicit model binding but I am getting the following error if I try to attach more than one model with methods.
index() must be an instance of App\\Http\\Models\\Modelname, string given
Here is my code:
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}
Route:
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
Your route should looks like that:
Route::resource("your_route/{model1}/{model2}/{model3}","MyController",[
"except"=>["create","edit"]
]);
Yess you can register routes like these
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
but in your controller, you have to
public function index($id,$id2,$id3)
{
print_r($application_endpoint);
}
OR
you can do like this
Route::model('key/key/key', 'MyController')
and in your controller
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}

Laravel policies - passing the class as a variable to $user->can() method doesn't work

I have a route with dynamic model recognition. In other words, I take the desired model as an argument and use it in the controller. I have complex authorization in my app and I need to pass the model class name as a variable to the $user->can() method for using policies, but for some reason it doesn't work. Here's my code:
Policy:
public function view($user, Model $model) {
return $user->model_id == $model_id;
}
public function create($user) {
return $user->isAdmin();
}
Controller:
public function createModel($model) {
$model_class = $model . '::class';
if (Auth::user()->can('create', $model_class)) {
return $model_class::create();
}
return 'invalid_permissions';
}
If I hardcode the model class name it works. For example, if my model is 'Car' and in the controller I put:
if (Auth::user()->can('create', Car::class)) {
Anybody got any ideas why this is so and how to fix it? I hope that it's possible because I would have to change my whole concept if it isn't.
*Note: this is example code, not my actuall classes

Passing Route Parameters to Filters when Using RESTful Controllers

I have searched around for long but nothing can quite fit my problem.
I am using RESTful controllers on my site. For some controller actions, some filters are needed and with this i do something like (i use the beforeFilter() function in the constructor):
<?php
class PostController extends BaseController {
public function __construct()
{
$this->beforeFilter('auth',array('only'=>array('getCreate','postCreate','getEdit','postEdit','postAddComment')));
$this->beforeFilter('csrf',array('only'=>array('postCreate','postEdit')));
// $this->beforeFilter('auth_post_edit', array('only'=>array('postEdit','getEdit')));
}
public function getIndex
{
$posts = Post::all();
return View::make('home')->with('posts',$posts);
}
public function getCreate()
{
return View::make('posts.create');
}
...
For the commented filter, however, it is meant to ensure that only the author of a post can edit the post, so i need to pass the post_id which is passed as a URI parameter, to the filter(or access it from the filter).
Some link showed how i can access parameters from the filter using the $route->getParameter('param') in the filter, but the problem is that because i have not even named my parameters(they are named in the controller actions), i am not able to access them from the filter using the above method.
So, how can i access route parameters from within the filter, or/and how do i name route parameters in RESTful controllers(not in their actions)?
You could use in your filter the Request::segment()
Route::filter('foo', function()
{
$param=Request::segment(2); //if the parameter is on the 2nd uri fregment.
$post=Post::find($param);
if ($post->author_id != Auth::user()->id)
{
return App::abort('404'); //or whatever
}
});

Dynamic router name for magento controller

How would I go about creating a custom module that has a controller with an action name that is dynamic, in the sense that it can be configured by the user in the admin area at will and be automatically updated in the custom module?
You can override this method in your controller:
public function getActionMethodName($action)
{
return 'indexAction';
}
public function indexAction()
{
//action name
var_dump($this->getRequest()->getActionName());
}
Then always will go to the index action, where you can use the original action name as a parameter.
then:
http://mysite/mymodule/mycontroller/im-dracula-blablabla
Will work!
I think you can approach this by using magic php method __call on your controller.
I assumed that you store your action name in a Magento config named 'mymodule/controller/action', so you can get the value using :
Mage::getStoreConfig('mymodule/controller/action');
Then you have the controller for example Mymodule/controllers/TestController.php
And you add the method in that controller like this :
public function __call($method, $arg) {
if ($method == Mage::getStoreConfig('mymodule/controller/action')) {
//Do whatever you want
}
}
This will make your controller //Do whatever you want when you accessing it using the action you specified in the config. The basic idea is like that. Hope this helps.

Resources