Reputation: 61
Is it possible to call a function in the controller without using a route or should I make a new route with two parameters as below that redirects to the specific page after the session has been added?
route::get('addsesion/{session-name}/{session slug};
If it's possible with ajax, can someone please point me in the right direction?
Basically what I would like to do is call the function addSession($session_name, $slug)
from a controller with ajax on link <a href/>
click , where it stores my specific session name and current page's slug.
It should call this addSession
function on a click, store session data and then redirect to a different url. e.g. /seeparts
, where it displays all saved session data.
Do I have to make a new route route::get('addsesion/{param1 - session-name}/{param2 - session slug}', currentController@addSession );
and then use that route as an ajax url? Or is there any other way how to use the controller's function?
My current Controller:
public function showAll() {
$parts = \DB::table() - > all();
$data = [
'parts' => $parts,
];
return view('partlist', $data);
}
public function showCpu($slug) {
// Specification query
$specs = \DB::table() - > select($select_columns) - > where('slug', $slug) - > first();
$data = [
'specs' => $specs,
'slug' => $slug
];
return view('part', $data);
}
//Add session - call this function
public function addSession($session_name, $slug) {\
Session::put($session_name, $slug);
}
}
part.blade.php:
<html>
@include('head.blade.php')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" >Add to partlist</a>
</body>
</html>
Upvotes: 3
Views: 601
Reputation: 5992
I think you can use Service Injection
binding controller function in your view.
Maybe you can reference it, https://laravel.com/docs/master/blade#service-injection.
For example:
<html>
@include('head.blade.php')
@inject('currentController', 'App\Http\Controllers\currentController')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" onClick="{{ $currentController->addSession($session_name, $slug) }}">HERE</a>
</body>
</html>
Upvotes: 2