Getting the controller? - laravel

In my view I create links via:
URL::action('NotSureWhatController#getIndex', 'id') }}
My view is a template that is used by a variety of different controllers, what's the best way to change the name of the controller in the action?
The only thing I can think of is setting a var in the controller and passing it through.
Is there a better way? Or a way to get the controller name?
I can't use 'as' in the route to name the controller either (as this is used for something else) so this won't work:
Route::currentRouteName()

Option 1
You should create the URL directly in the controller, then pass it as a variable to the view. The view will just print the url.
Option 2
You pass the name of the controller as a variable to the view (always from the controller), then you use the escape values of the blade templating to print it inside your function to generate URLs.
Option 3
Using the REQUEST class to get information about the page.

Related

how to use two functions from same controller in single page route using get in laravel

Am trying to use two different functions from one controller in a single page route
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
But the problem is the function alldata works where the function index doesn't
You can't have 2 GET routes with the same path.
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart/all','App\Http\Controllers\Frontend\CartController#alldata');
The /cart route is overwritten by the alldata(). So the alldata() is calling instead of index().
kindly remove the alldata()'s route and pass the data from index().
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
Try to manipulate your logic in controller rather than in route file.
Use conditional in controller function.

Using route() and with() in html view in Laravel 5.6

For redirection, in controllers, we can write like this:-
Redirect::Route('front_index')->with('RegError', 'An error occured while adding the user.');
How can I set up the href of an anchor tag so that I can send the 'RegError' too? Say something like this?
<a href="{{route('front_index')->with('RegError', '')}}">
actually you can't because with function is used to pass parameters from controllers to view and route helper doesn't have this method.
You need to make request and in controller pass your RegError

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

Passing data from one view to another in Laravel

I have a Controller that parses an XML and returns a view with a list of names and URLs.
return view('view_1',compact('myList'));
View_1 will have a form with parameters method="POST" action="goToView_2"
Then I get some information from my view_2 through a POST, but I still want to keep $myList so that view_2 view uses it aswell.
How do I pass $myList from the first view to the next through a controller?
It sounds like you're trying to have multi-step form of some kind.
I would store the data in the session and easily access it in the second controller or in the view (although not recommended).
https://laravel.com/docs/5.4/session#using-the-session
PS. I personally love using the global session helper.

Laravel, get the url for a controller/action from within the view

In my view I do not want to hardcode a url in just incase I change it... Is there a way to generate the hyperlink url by saying i'm going to use this controller and this action... something like:
<a href = 'echo ActionLink("Logout", "Authentication");'>Logout</a>
I also just found this...
Logout
What you need to do is to be able to refer to your routes somehow. There are two primary methods of doing this: naming them and referring to a controller action (i.e. Controller#action).
The best and most flexible, however, is to name your routes. This means that if you refactor your controllers (e.g. change the classnames or namespaces), you have to change less code (just where the route points to, rather than where each view reference is).
Whichever way you do it, you can use all sorts of helpers to get what you want. The following are all equivalent:
{{ link_to_route('route.name', 'Title) }}
{{ HTML::linkRoute('route.name', 'Title') }}
Title
Title
Similarly, you can use 'action' in place of 'route' in those helpers to do the equivalent version using the Controller#action way of specifying the route.

Resources