Trying to get intended url in LoginController in Laravel 5.6 - laravel-5

I'm trying to capture the intended url in my LoginController so I can execute some logic in a showLoginForm() method I added to the controller so I can send the user to a specific view based on the intended URL.
I've tried the following and I cannot get it to work:
public function showLoginForm()
{
$intededUrl Session::put('url.intended', URL::full());
// my base application url is http://www.websites.com:8080
if (starts_with($intededUrl, url('/admin'))) // i want all routes that begin with http://www.websites.com:8080/admin to go here
return view('auth.login');
return view('themes.'.env('APP_THEME', 'mango').'.auth.login'); // but it keeps taking me here
}
I'm using Laravels starts_with() method to try and match the start of the url string.

I just figured it out. I needed Session::get('url.intended');

Related

Laravel: Multiple Route to Same Controller

May I know how can I make just a single route so I don't have to repeat it? Thanks in advance.
Route::get('/url', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/url/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url/submitted', 'form.submit')->name('CtcForm.submit');
Route::get('/url2','CtcFormController#store')->name('CtcForm.eng');
Route::post('/url2/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url2/submitted', 'form.submit')->name('CtcForm.submit');
As per your given example, you want to handle the variable part of the route which is /url/ and /url12/. Yes! you can handle there both different route using a single route in ways:
Use route variable to handle dynamic url values i.e. url, url2,url3...url12 and so on.
Route::get('/{url}', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/{url}/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/{url}/submitted', 'form.submit')->name('CtcForm.submit');
Now in your controller methods handing above routes receive extra parameter $url like:
In controller CtcFormController.php class:
public function index(Request $request, string $url) {
//$url will gives you value from which url request is submitted i.e. url or url12
//method logic goes here ...
}
Similarly, method handling /{url}/submit route will be like:
public function submit(Request $request, string $url) {
//method logic goes here ...
}
Let me know if you have any further query regarding this or you face any issue while implementing it.

Laravel routes not working via GET while same route works via VIEW

I am trying to make a route that shows the user a secondary registration form, when i use this code:
Route::view('RegisterFamilyMembers', 'auth.registerFamilyMembers')->name('RegisterFamilyMembers');
The route works fine and routes to the file, but when i try to do the same via the GET method:
Route::get('RegisterFamilyMembers', 'Auth\FamilyMembersController#showRegistrationForm')->name('RegisterFamilyMembers');
And return the view in the controller:
public function showRegistrationForm()
{
return view('auth.registerFamilyMembers');
}
I get a RuntimeException page with no information on the problem.
does anyone know what can cause this problem?

Laravel 5.7 ApiResource GET params empty

I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello&param2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,
You can use:
$request->route("param");

How to get controller action by passing URL in laravel

I searched more time to find how to get the controller method name by passing the URL but not found my expected answer. I want to make a method where I will pass a URL and it will give the corresponding controller action like as below but I can't figure out.
I found a helper which just return the current URL's action which is Route::currentRouteAction()
If a route in my application like as Route::get('/abc', 'YourController#method') which will generate the url http://example.com/abc
then how can I get the YourController#method by passing http://example.com/abc
function getAction($url){
//what will be logic?
// return like App\Controllers\MyController#method
}
I have to make a custom permission system where I need it for show and hide the menu by checking the URL of each menu.
Within your controller you can do the following:
<?php
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
public function index(Request $request, Router $route)
{
$action = $router->getRoutes()->match($request)->getActionName();
// action should be what you're looking for.
}
You can try this if you want to:
Route::get('/the/url', 'YourController#method');
Every time anything calls the URL in the route, your method will be called.
You don't need to navigate to that url to call your method, it could be called by a form action, or a buttons action and just execute your method.
Edit:
url is your url as parameter (plain route)
import this:
use Illuminate\Routing\Route;
this is your function:
public function method(Route $route, $url)
{
$routes = \Route::getRoutes()->getRoutes();
foreach($routes as $r){
if($r->getUri() == $url){
$youraction= $r->getActionName();
dd($youraction);
}
else{
dd('does not exist');
}
}
}
Tested.

Laravel 4 - Setting up routes

I'm working on a site I have inherited and having a little trouble routing to a controller.
When I visit the URL www.domain.com/banners/statistics, it won't return anything.
I also noted that when I try and link to this page via Banner Statistics this also gives me an error on my home page.
Routes.php
Route::resource('banners', 'BannerController');
Route::get('banners/{banners}/activate', 'BannerController#activate');
Route::get('banners/{banners}/deactivate', 'BannerController#deactivate');
Route::get('banners/{banners}/delete', 'BannerController#delete');
Route::get('banners/{banners}/preview', 'BannerController#preview');
Route::any('banners/{banners}/cropresize', 'BannerController#cropresize');
Route::get('banners/statistics', 'BannerController#statistics');
BannerController.php
public function create()
{
$data['title'] = 'Create Banner';
$data['disciplines'] = Discipline::lists('name', 'id');
return View::make('admin.banners.create', $data);
}
public function statistics()
{
return View::make('admin.banners.statistics');
}
The resource controller provides you multiple routes.
Including :
GET /resource/{resource} redirecting to the show action of your controller.
List of all created routes : http://laravel.com/docs/controllers#resource-controllers
So when you call
banners/statistics
Laravel think you want to call the show action with "statistics" as a parameter.
To avoid this, you can put all your custom routes above your resource controller route.
Route::get('banners/{banners}/activate', 'BannerController#activate');
Route::get('banners/{banners}/deactivate', 'BannerController#deactivate');
Route::get('banners/{banners}/delete', 'BannerController#delete');
Route::get('banners/{banners}/preview', 'BannerController#preview');
Route::any('banners/{banners}/cropresize', 'BannerController#cropresize');
Route::get('banners/statistics', 'BannerController#statistics');
Route::resource('banners', 'BannerController');
This way Laravel will call your custom route before the routes created by your resource controller.
You can also use only and except if you don't need some of the resource controller routes.
Route::resource('banners', 'BannerController',
array('except' => array('show')));

Resources