Laravel route group is not taken properly - laravel

I created 2 route groups in my api route file, and I can't access to routes of second group.
First group :
$api = app('Dingo\Api\Routing\Router');
$request = app('\Dingo\Api\Http\Request');
$api->dispatch($request);
/* API V1 ROUTES */
$prefix = 'test';
$api->group(['prefix' => $prefix,'version'=> 'v1'],function($api) {
$api->post('/entity/validate/{code}', [
'as' => 'validate.code',
'uses' => 'App\Http\Controllers\XXX\Api\SmsController#validateCodeV1',
]);
});
This is working perfectly and I reach my function validateCodeV1 that does perfectly its job.
On another side I have right after it
Second group :
$prefix = 'testv2';
$api->group(['prefix' => $prefix,'version'=> 'v2'],function($api) {
$api->post('/entity/validate/{code}', [
'as' => 'validate.code',
'uses' => 'App\Http\Controllers\XXX\Api\SmsController#validateCode',
]);
});
Here I have a 404 when I try to call my api with a prefix testv2/entity/validate/XXX
I don't know how I can specify properly my prefix to swap from my v1 route to my v2 route...
I use Laravel 5.5
EDIT :
check of routes contains only one of my 2 routes, even after cache clear :
php artisan route:list | grep entity/validate
| | POST | /test/entity/validate/{code} | validate.code | Closure | api.controllers |

I've tried your code and change the $api-> to Route:: and both work as they should and listed in php artisan route:list.
$prefix = 'test';
Route::group(['prefix' => $prefix,'version'=> 'v1'],function($api) {
Route::post('/entity/validate/{code}', [
'as' => 'validate.code',
'uses' => 'App\Http\Controllers\XXX\Api\SmsController#validateCodeV1',
]);
});
$prefix = 'testv2';
Route::group(['prefix' => $prefix,'version'=> 'v2'],function($api) {
Route::post('/entity/validate/{code}', [
'as' => 'validate.code',
'uses' => 'App\Http\Controllers\XXX\Api\SmsController#validateCode',
]);
});
I think, you can check your $api or $request, maybe there is an error, function, or something else that only allow v1 version.

I fixed it by mixing the 2 solutions of this post :
Dingo API - How to add version number in url?
It is not exactly a duplicate so I let my post, but if someone think it is appropriated to remove my post just ask and I will do it

Related

Force Route::group(['domain'=>'external.com']) to return https-prefixed routes

I created a new routes definition called external.php.
This is how it looks like:
Route::group([
'domain' => 'example.org'
], function () {
Route::post('oauth/token')->name('external.oauth.token');
This works fine, so php artisan route:list contains:
| example.org | POST | oauth/token | external.oauth.token | Closure | |
If I do route('external.oauth.token') I am getting this result:
"http://example.org/oauth/token"
So my question is: How can I force the route to be secure/with https-prefix?
You could force this group of routes to be secure:
Route::group(['domain' => ..., 'https'], function () {
...
});
Or for just that one route:
Route::post('oauth/token', ['uses' => ..., 'https'])->name('external.oauth.token');
For an actual external URL you could probably just add this URL to a config file and access it with a helper by name.
config/urls.php:
<?php
return [
'external' => [
'oauth' => [
'token' => 'https://example.org/oauth/token',
]
],
];
Helper method:
function urls($name)
{
return config('urls.'. $name);
}
Where needed:
$url = urls('external.oauth.token');
You can name the method and the config and the keys as you wish, this was just an idea to match the name you used.

What does 'as' method do in Laravel

In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});

Cookie::get returns null, laravel 5.4

I'm trying to cookie user login values,
I grouped in route like this:
Route::group(['middleware' => ['admin']], function () {
Route::post('/admin/addArticle', [
'as' => 'article_save', 'uses' => 'AdminController#saveCover'
]);
Route::get('/admin/introduction', [
'as' => 'introduction', 'uses' => 'AdminController#introduction'
]);
});
AdminController:
$cookie = Cookie::forever('admin', $admin);
Cookie::queue($cookie);
return Redirect::route('introduction')->withCookie($cookie);
Models/Admin:
if (Cookie::has('admin')) {
//echo 'admin is not in session but cookie';
$admin = Cookie::get('admin');
//...
but it's not go in this if never and nothing is saved in cookie !!!
Unfortunately I have upgraded to laravel 5.4 of 5.2 and anything is in the wrong way now :((((
please help me!
Laravel is encrypting cookies, so I had to add an exception for them in App\Http\Middleware\EncryptCookies\:
protected $except = [
'cookie_name'
];
Just change the way you set the cookie to Cookie::queue('admin', $admin);

Get a parameter from a route?

I have the following route:
Route::get('news', [
'as' => 'news',
'uses' => 'ArticleController#getIndex',
]);
There are also other routes that use the same resource controller, e.g:
Route::get('travel', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
In the article controller, how can I get the category of the articles. e.g. travel or news?
I can't turn it into a route param e.g.
Route::get('{travel}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
As there are other sections such as contact, faqs, etc that do not use the article controller.
I know I could put it all on one route:
Route::get('/article/{category}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
But I want pretty URLS like:
mydomain.com/category-slug/article-slug
Ideally if I could use a resource controller:
Route::resource('news', ArticleController');
With some sort of array:
Route::resource(['news', 'travel'], ArticleController');
So my questions:
How can I get the name of the resource. e.g. news, travel in my controller?
Is there an easy way to specify varying routes to the same resource?
For Example Keep your routes like this as your static routes will be limited and need to be unique so they will not get conflict between routes
demo routes :
Route::get('contact', [
'as' => 'contact',
'uses' => 'contactController#getIndex',
]);
Route::get('{travel}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex',
]);
In order to achieve a route like that:
mydomain.com/category-slug/article-slug
Use the following:
Route::get('/{category}/{article}', 'ArticleController#getArticle');
Then in your controller, you will have 2 parameters in your getArticle method.
public function getArticle($category, $article)
{
//Your code here
}
In order to have pretty urls you need to make a few steps:
1)
First of all you need to add a column to your articles table called for example 'slug'.
2) Then you need to specify your route key in your article model. Like this:
Article model
public function getRouteKeyName()
{
return 'slug';
}
3) When you add an article you should create the slug by your own in order to be unique. so in your controller that you create the article add this code.
$slug = str_slug($article['title'], '-');
$slugs_count = DB::table('articles')->where('name',$article['title'])- >count();
if($slugs_count>1){
$slug = $slug.'-'.($slugs_count-1);
}
$article->slug = $slug;
$article->update();
4) Now we have to set the route
Route::get('/article/{slug}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex'
])->where(['slug','[\w\d\-\_]+']);
5) Finally in order to get the article you need
$article = Article::where('slug','=',$slug)->first();
If you want to use categories etc you could pass more parameters in your route and manipulate it in your controller. like this
Route::get('/article/{category}/{slug}', [
'as' => 'travel',
'uses' => 'ArticleController#getIndex'
])->where(['slug','[\w\d\-\_]+','category','[\w\d\-\_]+']);
The where clause is to restrict the parameter with some regular expressions(its optional).
I forgot to mention that the routekeyname in your model works in laravel 5.2.

Routing confusion in laravel 4

I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.
Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.
Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.

Resources