Adding in a variable via route with static route laravel - php

I want to be able to add a variable to a route that directs to a controller.
The only way I've seen it done is if the route itself is dynamic. However, in my case, I want a static url that will send a static variable to a controller. The reason for which is that there will be two static routes that will use the same controller in a different way via variables.
I am having trouble finding this in the documentation.
All I have found is this
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
I want to do the same but have {comment} and {post} but two variables is sent to the controllers.
Any help would be awesome!

Forexample:
Route::get('posts/{post}/comments/{comment}', ['as'=> 'viewComment', 'uses'=>'PostController#viewComment']);
Controller:
public function viewComment ($post, $comment)
{
dd($post.'/'.$comment);
}

Related

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Passing parameter / variable through two controllers in Laravel difficulty

I have a Data model that I want people to be able to view individual records of and then edit/add data to. I have managed to get the view route working;
Route::get('/data/{data_token}', 'DataController#show');
Data_token, is a unique string. This then uses this DataController function;
Public function show($data) {
$data = Data::where('data_token',$data)->first();
return view('data.show', compact('data'))
}
After which I can display the data on the page, and have a form for editing (actually its for adding data that doesn't exist, but whatever, same principle right).
On the form on the data.show view, I am sending it to a different view;
Route::get('/data/{data_token}/edit', 'DataController#edit');
This can use the $request variable to return the forms values, but I can't relate it to the data row I was previously editing?
how do I get the {data_token} passed to the edit function of the controller?
Edit( adding route files)
Noticed I forgot the {'data_token'} in the post route.
/Begs forgiveness
I think you've misunderstood how the routes and controllers work. What you're looking at is a fairly simple CRUD setup like the following;
Route::get('/data/{data_token}', 'DataController#show');
Route::get('/data/{data_token}/edit', 'DataController#edit');
Route::post('/data/{data_token}/edit', 'DataController#update');
Now your controller would have;
public function show($dataToken) { ... }
public function edit($dataToken) { ... }
public function update($dataToken, Request $request) { ... }
Then you'd have your form on the edit view like so;
<form action="{{ route('DataController#update') }}" method="post">
Laravels router will always try to pass in the URI variables as arguments to the methods provided.
Providing that I have understood what you need, this should suffice.

Passing variable from backend to frontend laravel

I have 5 controllers and 5 models and they are all related to backend. I can easily output data in the backend but I need to that for the frontend as well. Not all of course but some of them.
For example I have controller called BooksController:
public function getBooks(Request $request)
{
$books = Books::all();
return view('backend.books.show', compact('images'));
}
So this will show it in backend without any problems but what I want is for example to loop through all the books and show their images in welcome.blade.php which doesn't have controller.
And also to pass other parameters to that same view from different controllers.
Is this is possible?
Thank you.
You are having an error because you did not declare the variable $image
public function getBooks(Request $request)
{
$books = Books::all();
$images = array_map(function($book) {
$book->image;
}, $books);
return view('backend.books.show', compact('images'));
}
It sounds like you are potentially caught up on some terminology. In this case, it sounds like backend is referring to your admin-facing interface, and frontend is referring to your user-facing interface.
You also seem to be locked on the idea of controllers. Unless the route is verrrrrry basic, create a controller for it.
Have a controller for your welcome view, for your admin view, basically (with some exceptions) a controller per resource or view is fine.
In this case, you would have one controller for your admin book view, and a seperate controller for your welcome view. Both of which would pull the books out of the db and render them in their own way

How to call a function of a controller from a view?

There is a controller :
use Phalcon\Mvc\Controller;
class ReferentielClientController extends Controller
{
public function indexAction(){
// moteur de template pour une View
$this->view->act_form = 'ReferentielClient/ajouterClient';
return $this->view->pick("client/listerClient");
}
public function ajouterClientAction(){
$this->view->action_form = 'ajouterClientExec';
return $this->view->pick("client/ajouterClient");
}
...
}
From a view I want to call the indexAction method of the controller ReferentielClientController. How to do that ?
Short answer: You can't.
You can't since indexAction is not a normal method, it's an action. Actions aren't called by anyone in the usual way that methods are, they are called with routes an url. Let me explain this a little bit further:
Using MVC, we have the following route using :
http://www.example.org/a/b/c
a is which we call module.
b is which we call controller.
And c is which we call action.
Index actions and default modules will not appear in the URL.
So, how to call functions from view? Better not to. Best way to proceed is to do everything in your controller and pass that info to the view. I.e.:
public function indexAction(){
$this->view->myInfo = $allMyInformationInOneObject;
}
And then, in the view (index.phtml):
<p>
<?php echo $this->myInfo ?>
</p>
Summary:
You don't need to call any controller function from the view, just give it that info from the controller.
Added info about redirection
OK, answering to your commentary. What you want is not to call that function but to redirect the browser to that action.
So, I'll suppose you have your route created in your router, if not, please, go to the Zend documentation about routing. It's highly recommended to create routes instead of using the URL itself.
I guess that you will not be able to use the normal redirecting using href on a button or such, so you will need to use the redirector helper. Since you are not in a controller, you are not able to use the helper by using:
$this->_helper->redirector->gotoUrl($url);
So you will need to instance the redirector like this:
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
$redirector->gotoUrl($url);
Being this $url, the URL given by your router, as you can read in the link above.
This can be done but I strongly recommend not to redirect from the view but from the controller. It would be good that all the logic remains in the controller.
I hope this helps!
Ok I found it : I set the href to point to the controller : Annuler

pass user information in every function of the controller in laravel

I have a Controller witch contains more than 150 functions. almost all of the functions inside the controller are returning views.
each view should change if the user is logged in.
I almost handled this in the view side by using parent blades and etc. But in the controller side for almost every function i should check if the user is logged in and return some specific data about the user along with the view.
for example:
$user=[];
if(Auth::check())
{
$user=Auth::user;
$reputation=$user['reputation'];
$messages=$user->messages();
$notifications=$user->notification();
}
return view('pages.profile')->with(['user'=>$user , 'messages'=>$messages , 'notifications'=>$notifications]);
I thought that this is possible using middlewares but i could not figure it out.
what are the alternatives here?
for more information i am using laravel 5.
One solution could be using your custom function instead of view(). Sth like:
//in your controller action
public function someAction() {
return $this->view('pages.profile');
}
protected function view($template, $data = array()) {
return view($template)->with($data)->with([data that needs to be passed to all views]);
}

Categories