User defined Function works in Route::get() but not Route::group() - laravel

I am using laravel 7 and created a function to include all route files includeRouteFiles() in a directory. Im just trying to figure out why this works:
Route::get('/', function () {
includeRouteFiles(__DIR__ . '/someFolder/');
});
and this does not:
Route::group(['namespace' => 'SomeSpace'], function() {
includeRouteFiles(__DIR__ . '/someFolder/');
});
this is located in the web.php file under the routes directory.
I assume the that the code in the ServiceProvider where the functions lives is correct because it works in Route::get().
thank you

Related

View::composer not working on boot() service provider

Inside a Laravel Package EspacePartenaire I need to share some data between all its views using View Composer.
Ths boot() method inside EspacePartenaireServiceProvider containes the following:
$this->loadViewsFrom(__DIR__ . '/views', 'espace-partenaire');
View::composer('*', CurrentPartenaireComposer::class);
But I don't want to share data in all the views. I need it only in the views that are located in the view folder of the Package /packages/EspacePartenaire/src/views
When I change arguments of the composer function like below:
View::composer('espace-partenaire', CurrentPartenaireComposer::class);
or
View::composer('espace-partenaire::*', CurrentPartenaireComposer::class);
I had the error that my variables are undefined.
How can I achieve this?
EDIT:
This is the Package routes file:
Route::group([
'middleware' => ['web', 'auth'],
'namespace' => 'App\Services\EspacePartenaire\Http\Controllers',
'prefix' => 'espace-partenaire'
], function(){
...
Route::get('/', 'EspacePartenaire#index');
...
});
Based on your packages structure i think the following approach could maybe work, it seems first parameter is the path and you can do * as a wildcard.
View::composer('*espace-partenaire*', CurrentPartenaireComposer::class);

why i see Call to undefined method Error?

im learning laravel so if im not well as you are accept my apologies...
my problem is when i try to define a new method in web.php i got error!some times phpstorm sets problem on 'Route'word so i can run my blade pages but sometimes it sets problem on 'get','post','group' ,...and i cant run my app
ill show you how i defined my routes
use Illuminate\Routing\Route;
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'] ,function (){
Route::get('/users','UsersController#index');
});
after all this my point is making a controller so i got this error to fix so i can move on...
Named groups in laravel
Route::group(['prefix'=>'admins','as'=>'admin.'], function(){
Route::get('users', ['as' => 'user', 'uses' = > 'UsersController#index']);
});
Also make sure you have index method in your UsersController.
FYR :- https://laraveldaily.com/laravel-5-1-names-for-route-groups/

Laravel 5 - Named route with parameter

In my web.php file, I created two routes :
Route::get('/{name}', 'PublicController#index')->name('welcome');
Route::get('stats', function () { return route('welcome', 'enrique'); });
My controller looks like:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PublicController extends Controller
{
public function index($name)
{
return view('welcome');
}
}
I already set up a virtual host in my local machine which is http://blog.test
When I Call http://blog.test/stats in my browser, it shows me the content of my homepage. But when I reorganize my twho route in web.php file in that way
Route::get('stats', function () { return route('welcome', 'enrique'); });
Route::get('/{name}', 'PublicController#index')->name('welcome');
It works fine.
Can you please explain why it behaves like that? Thanks
What you have is the same route being overwritten. In order to have them both work, you will have to add something before your custom parameter:
/something/{name}
Otherwise stats is assumed to be value for your parameter name
Laravel route goes to first matched routes, so in your case if it sees /stats
Route::get('/{name}', 'PublicController#index')->name('welcome');
It becomes variable $name for PublicController#index
Check more article regarding this
It is happening because , when you add your {parameter} as after / , all routes defined after that, are considered as of that type
Route::get('/{name}', 'PublicController#index')->name('welcome');
// below routes not work
Route::get('stats', function () {});
Route::get('test', function () { });
Route::get('hello', function () {});
same thing happen if you create new route like below :
Route::get('post/{slug}', function () {});
// this get routes are also not work
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
so it is good practice that always define parameterised route at the
last
Route::get('post/show', function () {});
Route::get('post/preview', function () {});
Route::get('post/{slug}', function () {});

How to add dynamically prefix to routes?

In session i set default language code for example de. And now i want that in link i have something like this: www.something.com/de/something.
Problem is that i cant access session in routes. Any suggestion how can i do this?
$langs = Languages::getLangCode();
if (in_array($lang, $langs)) {
Session::put('locale', $lang);
return redirect::back();
}
return;
Route::get('blog/articles', 'StandardUser\UserBlogController#AllArticles');
So i need to pass to route as prefix this locale session.
If you want to generate a link to your routes with the code of the current language, then you need to create routes group with a dynamic prefix like this:
Example in Laravel 5.7:
Route::prefix(app()->getLocale())->group(function () {
Route::get('/', function () {
return route('index');
})->name('index');
Route::get('/post/{id}', function ($id) {
return route('post', ['id' => $id]);
})->name('post');
});
When you use named routes, URLs to route with current language code will be automatically generated.
Example links:
http://website.com/en/
http://website.com/en/post/16
Note: Instead of laravel app()->getLocale() method you can use your own Languages::getLangCode() method.
If you have more questions about this topic then let me know about it.
Maybe
Route::group([
'prefix' => Languages::getLangCode()
], function () {
Route::get('/', ['as' => 'main', 'uses' => 'IndexController#index']);
});

How to remove missingMethod route when using Route::controller() in Laravel 4?

When I use Route::controller() it will automatically generate a route like: GET|HEAD|POST|PUT|PATCH|DELETE resource/{_missing}.
this will make conflict if I have route like resource/{id}/somethingElse.
Sample code
<?php
Route::controller('page', 'PageController');
Route::Group(['prefix' => 'page/{id}/comments'], function() {
Route::get('/', 'CommentController#index');
Route::post('/', 'CommentController#create'); // <-- this will not work sometimes I don't know why
});
The line I highlighted throws NotFoundHttpException with Controller method not found. message.
Is there anyway to remove that route containing {_missing} parameter?

Resources