I"m using Laravel framework and I have situation adding routes based on different conditional parameters.
Currently, I'm using this code.
Route::get('/{report?}/{type?}', [
'uses' => 'SomeController#getReport'
])->where(['report' => 'overview', 'type' => 'type1']);
www.example.com/overview/type1 // Working
www.example.com?report=overview&type=type1 // Not working (not verifying the where conditions).
I have another solution to resolve this. Is this a better way?
if (Input::get('report') == 'overview' && Input::get('type') == 'type1') {
Route::get('/', ['uses' => 'SomeController#getReport']);
}
Try this:
if (request()->get('report') == 'overview'
&& request()->get('type') == 'type1') {
Route::get('/', [
'uses' => 'SomeController#getReport'
);
}
Try this. Hope it will work.
Route::get('/{report?}/{type?}', function()
{
if (Input::get('report') == 'overview'
&& Input::get('type') == 'type1') {
// Run controller and method
$app = app();
$controller = $app->make('SomeController');
return $controller->callAction('getReport', $parameters = array());
}
});
Related
In Laravel 5.8 app using tymon/jwt-auth 1.0.0
I have users_groups table and I need for logged user for some controller to make check if inside of some group.
For this in routes/api.php I have :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'manager', 'as' => 'manager.'], function ($router) {
Route::get('users_of_event_selection/{event_id}', 'API\ManagerController#users_of_event_selection');
Route::post('add_user_to_event', 'API\ManagerController#add_user_to_event');
...
I app/Http/Controllers/API/ManagerController.php I added checks:
public function __construct()
{
$this->middleware('jwt.auth', ['except' => []]);
$request = request();
$this->requestData = $request->all();
$loggedUser= Auth::guard('api')->user();
$userGroupsCount = UsersGroups
::getByUserId($loggedUser->id)
->getByGroupId([ACCESS_ROLE_ADMIN,ACCESS_ROLE_MANAGER])
->count();
if($userGroupsCount == 0) {
return response()->json(['error' => 'Unauthorized'], 401);
}
}
But
$userGroupsCount == 0
the code above does not work as I expected and my control's method returns valid data. I suppose I can make
small function and to call it in top on any control's method, but if that ig good way? If jwt-auth has any way to extend
additive checks ?
Thanks!
is there a simple way to translate my routes in Laravel 5.4. My translation files located in here:
/resources
/lang
/en
routes.php
/de
routes.php
In my web.php i define my routes like this:
Route::get('/{locale}', function ($locale) {
App::setLocale($locale);
return view('welcome');
});
Route::get('{locale}/contact', 'ContactController#index');
I have found very elaborate solutions or solutions for Laravel 4. I am sure that Laravel has also provided a simple solution. Can someone explain to me the best approach?
Thanks.
we usually do it like this
to get the current language:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'nl';
}
routes:
Route::group(['prefix' => $language], function () {
Route::get(trans('routes.newsletter'), array('as' => 'newsletter.index', 'uses' => 'NewsletterController#index'));
I created a file translatable.php in my config folder:
<?php
return [
'locales' => ['en', 'de'],
];
web.php:
$request = request()->segment(1);
$language = null;
if (!empty($request) && in_array($request,config('translatable.locales'))) {
$language = $request;
App::setLocale($language);
} else {
$language = 'de';
}
Route::get('/', function() {
return redirect()->action('WelcomeController#index');
});
Route::group(['prefix' => $language], function () {
/*
Route::get('/', function(){
return View::make('welcome');
});
*/
Route::get('/',
array( 'as' => 'welcome.index',
'uses' => 'WelcomeController#index'));
Route::get(trans('routes.contact'),
array('as' => 'contact.index',
'uses' => 'ContactController#index'));
});
Works fine - Thanks. Is the redirect also the best practice?
Best regards
The problem that I am having is that if I pass a value of an optional parameter in my get request, I get the same result that I would if no value was passed.
Route:
Route::get('/play/game/{new?}', [
'uses' => 'GameController#getGame',
'as' => 'game'
]);
Controller:
public function getGame($new = null) {
$quotes = Quote::orderBy('created_at', 'dec')->paginate(50);
if(!is_null($new)) {
if ($new == 'yes')
$newgame = true;
} else {
$newgame = false;
}
return view('game', ['quotes' => $quotes, 'newgame' => $newgame]);
}
View:
#if($newgame ==true)
{{Session::flush()}}
#endif
#if($newgame == false)
<h1>Not a new game</h1>
#endif
The second if statement in the view is just a test, and it is this statement that always executes.
For reference, I am using Laravel 5.2
You can register the route as follows:
Route::get('play/game/', [
'uses' => 'GameController#getGame',
'as' => 'game'
]);
and then when you call the url, pass the variable like this:
play/game/?new=yes
Then in your controller, you can check if the variable is set:
if($request->input('new')=='yes'
to check if the parameter is present:
$request->has('new')
try this
i think there is no need to put '?' symbol in parameter
Route::get('play/game/{new}', [
'uses' => 'GameController#getGame',
'as' => 'game'
]);
I'm trying to handle basic validation of my API calls in the Laravel's routes. Here is what I want to achieve:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 1 to the controller
});
Route::get('waiting', 'PropertiesController#getPropertyByProgressStatus', function () {
//pass variable x = 2 to the controller
});
});
Long story short, depending on the segment of the URI after api/v1/properties/ I want to pass a different parameter to the controller. Is there a way to do that?
I was able to get it to work with the following route.php file:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('remodeled', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 1
]);
Route::get('pending', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 3
]);
Route::get('available', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 4
]);
Route::get('unavailable', [
'uses' => 'PropertiesController#getPropertyByProgressStatus', 'progressStatusId' => 5
]);
});
and the following code in the controller:
public function getPropertyByProgressStatus(\Illuminate\Http\Request $request) {
$action = $request->route()->getAction();
print_r($action);
Pretty much the $action variable is going to let me access the extra parameter that I passed from the route.
I think that you can do it directly in the controller and receiving the value as a parameter of your route:
First you need to specify the name of the parameter in the controller.
Route::group(['prefix' => 'api/v1/properties/'], function ()
{
Route::get('{parameter}', PropertiesController#getPropertyByProgressStatus');
In this way, the getPropertyByProgressStatus method is going to receive this value, so in the controller:
class PropertiesController{
....
public function getPropertyByProgressStatus($parameter)
{
if($parameter === 'purchased')
{
//do what you need
}
elseif($parameter === 'waiting')
{
//Do another stuff
}
....
}
I hope it helps to solve your issue.
Take a view for this courses: Learn Laravel or Create a RESTful API with Laravel
Best wishes.
----------- Edited ---------------
You can redirect to the route that you want:
Route::group(['prefix' => 'api/v1/properties/'], function () {
Route::get('purchased', function () {
return redirect('/api/v1/properties/purchased/valueToSend');
});
Route::get('waiting', function () {
return redirect('/api/v1/properties/waiting/valueToSend');
});
Route::get('purchased/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
Route::get('waiting/{valueToSend}', PropertiesController#getPropertyByProgressStatus);
});
});
The last two routes response to the redirections and send that value to the controller as a parameter, is the most near that I think to do this directly from the routes.
I want to set the localization using subdomains. I've managed to set up subdomain wildcards and it's working fine. However I'd like to set up filters.
For example I was thinking of setting up an array of available countries in the config:
<?php
return array(
'available' => array(
'uk',
'fr',
'de'
)
);
Then in my routes I need a way of filtering a group. For the moment my code is the following without any filters:
<?php
$homeController = 'MembersController#profile';
if ( ! Sentry::check())
{
$homeController = 'HomeController#index';
}
Route::group(['domain' => '{locale}.'.Config::get('app.base_address')], function() use ($homeController)
{
Route::get('/', ['as' => 'home', 'uses' => $homeController]);
Route::post('users/register', ['as' => 'register', 'uses' => 'UsersController#register']);
Route::resource('users', 'UsersController');
});
Does anyone have any ideas for filtering the group?
Also if the subdomain isn't valid how can I redirect to something like uk.domainname.com?
Thank you in advance for any help, it's much appreciated.
you could solve this in your routes with a filter, that will be executed first. it checks then for the available subdomains and if it doesn't find it, it redirects to a default subdomain.
Route::filter('subdomain', function()
{
$subdomain = current(explode('.', Request::url()));
if (!in_array($subdomain, Config::get('app.countries.available'))) {
return Redirect::to(Config::get('app.default_subdomain') . '.' . Config::get('app.base_address'));
}
});
Route::group(['before' => 'subdomain'], function()
{
...
}
In your app/filters.php I would write something like this. You will have a to create a new variable in your config called availableSubdomains with your subdomains array.
<?php
Route::filter('check_subdomain', function()
{
$subdomain = Route::getCurrentRoute()->getParameter('subdomain');
if (!in_array($subdomain, Config::get('app.availableSubdomains')))
return Redirect::home();
});
Then I will add a before filter in your group route in app/routes.php
<?php
Route::group(
['domain' => '{locale}.'.Config::get('app.base_address'),
'before' => 'check_subdomain']
, function() use ($homeController)
{
Route::get('/', ['as' => 'home', 'uses' => $homeController]);
Route::post('users/register', ['as' => 'register', 'uses' => 'UsersController#register']);
Route::resource('users', 'UsersController');
});
Sorry, I haven't tested it.
For subdomains where the language designator is the first part of the subdomain name, like: en.domain.com/section here is what I've used for laravel 5.1
this uses getHost which avoids the problem with http/https showing up first
app\Http\routes.php:
Route::filter('subdomain', function() {
$locale_url = current(explode('.', Request::getHost()));
if (!in_array($locale_url, Config::get('app.countries_available'))) {
return Redirect::to(Config::get('app.default_subdomain') . '.' . Config::get('app.base_address'));
}
App::setLocale($locale_url);
});
it is necessary to add things like:
config/app.php:
'countries_available' => ['en','es'],
to find the variable
finally, to add it to a route
app\Http\routes.php:
Route::group(['before' => 'subdomain'], function()
{
Route::get('/', 'control#func');
...
});