laravel 4 workbench routing to the package - php

I have been working with laravel 4 for some time now and i needed to create an admin area so i decided to use a package to keep things all organized and separated from the rest of the application.
So i created a package with composer as 'vendor/admin'.
then i added those lines as documemented on laravel site
AdminServiceProvider.php
public function boot()
{
$this->package('vendor/admin', 'admin');
include __DIR__.'/../../routes.php';
}
public function register()
{
//
$this->package('vendor/admin');
}
I also created a routes.php file in vedor/admin/ directory to route all admin area in this file.
following i run the 'php artisan dump-autoload' and i finalized with this commend on artisan 'php artisan config:publish vendor/admin'
now i wanna be able use this package for mysite.com/admin route and i want the routes.php file in the package to render the routing for that URI, to do that:
Do i need to modify my app/routes.php?
How can i make vendor/admin/src/routes.php file to do the routing for all mysite.com/admin routes?
Thanks.

No you don't need to edit app/routes.php. As long as it doesn't contain any admin routes that could collide with the ones in your package you can leave it that way.
The routes file in a package can be used like the "normal" app/routes.php. An easy way to deal with admin routes is to have a prefix group:
Route::group(array('prefix' => 'admin'), function(){
// all your admin routes. for example:
Route::get('dashboard', '...');
// will match GET /admin/dashboard
});
Besides that, make sure you're package gets loaded correctly! One part being registering the service provider. Assuming the namespace of your package is Admin you need to add Admin\AdminServiceProvider to the providers array in app/config/app.php. More information

Related

How do you add a route to Fortify in Laravel 8?

I have Laravel with Jetstream installed. How can I add a route to Fortify?
I've read through the whole readme:
https://github.com/laravel/fortify/blob/1.x/README.md
That readme provides ways to customize functionality but it doesn't show a way to add a new route to Fortify.
I can see the routes.php file in
/vendor/laravel/fortify/routes/routes.php
but you're not supposed to edit stuff in the vendor folder. If you edit anything inside the vendor folder, whenever you run a Composer update it will overwrite any of your changes when the files update.
Typically I think you'd have to do some sort of artisan command to get proper access to the corresponding files by publishing Fortify's resources like:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
This would publish Fortify's actions to your app/Actions folder, etc
How can I add a new route to Fortify in the right way?
You should never touch or mess with vendor as it is immaculate.
By default the fortify routes located on /vendor/laravel/fortify/routes/routes.php, but you shouldn't edit anything inside the vendor folder otherwise whenever you run composer update it will overwrite any of your changes when the files update.
You can simply do the same on /routes/web.php with fortify middleware :
Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
// with fortify guest middleware
Route::get('foo', function () {
return 'Foo';
})->middleware(['guest']);
// with fortify auth middleware
Route::get('bar', function () {
return 'bar';
}) ->middleware(['auth']); // fortify auth middleware
});

Unable to list laravel routes because target class not found

I am currently working on an API only laravel application. In the controllers folder, there is an API folder that holds all controllers. The ForgotPasswordController is in the API folder as well.
When I run the command php artisan route:list I get the error below
Illuminate\Contracts\Container\BindingResolutionException : Target class [App\Http\Controllers\Auth\ForgotPasswordController] does not exist.
There is actually no ForgotPasswordController in the Auth folder. How do I handle this issue?
You have to make sure you are doing php artisan route:cache priority.
If the problem still persists, can you disable the auth provider and try?
For the sake of time considering that the project is live and I need to churn out a couple of features, I have moved both ForgotPassword and ResetPassword controllers back into the Auth folder. Ran a test to make sure nothing has been broken (everything works fine) and now I am able to list out the routes.
If you have Auth::routes() or Route::auth() in your routes file that would be generating routes to the ForgotPasswordController.
You would need to not be calling that or you would need to pass the proper option to it to have it not register those routes:
Auth::routes(['reset' => false]);
Depending on the version you are using this may not work. If that is the case you will have to not use this method at all and register the routes you want/need yourself.

Splitting Laravel / Lumen routes in multiple files

Lumen framework comes with the routes/web.php file. Reading about how to split the routes in multiple files I came across Laravel documentation (not Lumen) and there it seems pretty clear.
#see https://laravel.com/docs/6.x/routing#basic-routing >>> The Default Route Files
It states
All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by the framework. The routes/web.php file defines routes that are for your web interface. ...
Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class
So you can just add other route files and edit the app/Providers/RouteServiceProvider.php class, this seems pretty straight and clear.
Just that Lumen doesn't have any app/Providers/RouteServiceProvider.php class
So then what's the best way to define your own route files without mangling the framework?
Thanks!
We can do this just like Laravel.
Make routes directory in root folder.
Inside routes directory create files, for instance,
like routes/users.php, routes/posts.php
Add above route files, in bootstrap/app.php file
// Load The Application Routes
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
require __DIR__.'/../routes/users.php'; // mention file names
require __DIR__.'/../routes/posts.php';
});
The equivalent in Lumen is located in /bootstrap/app.php.
You can add routes file entries there appropriately. As you can see, there isn't really a specific API for adding files or anything. So just write the logic as you see fit.
If your routes are located in another folder like app/Api/V1 and your controller are located in app/Api/V1/Controllers then you can use the below code in bootstrap/app.php
Folder Structure
For route : app->Api->V1->routes.php
For Controller : app->Api->V1->Controllers
Code:
$app->router->group([
'namespace' => 'App\Api\V1\Controllers',
], function ($router) {
require __DIR__.'/../app/Api/V1/routes.php';
});

Routes in a package for Laravel

I am having a problem with accessing the Auth::user() in a Laravel package routes file.
I am creating a package for Laravel (using 5.8.9). The package has its own routes in a route file which is being added correctly.
I have created an identical route in the main test site and the package. I am using composer 'path' to symlink the package into the test site which is on Homestead that I'm using on a windows 10 computer.
The route is simple and I'm just checking to see if a user is logged in, which it is.
Route::get('/home', function () {
dd(Auth::check());
});
In the main site routes file the check is returning true as expected. When commented out and allowing the route file in the package to handle it we get a return of false.
I noticed it as was getting an error when trying to run middleware on the package and after some investigating found that the package is not registering the user as being logged in.
Any ideas? Is it a problem because I'm using the path on composer to symlink?
EDIT: OK I've ruled out the symlink being the problem by creating a private repo and importing it. So its now in the vendor as a normal imported package but still getting the same issue.
EDIT2: I just tried to pull a user in the package routes file by just doing a simple
Route::get('/home', function () {
$user = \App\User::find(1);
dd($user);
});
And get null does anyone know if its possible to access the user/core laravel models in a package or is it disabled or a problem with the load order?
I managed to resolve it, if you use routes that require auth in your packages then you need to include the web middleware. If you do not include the web middleware then you are not loading some of the functionality that the main framework will load, like sessions.
And if you don't have sessions then obviously you wont be able to retrieve logged in users.
So to get it working you would change the code to
Route::get('/home', function () {
$user = \App\User::find(1);
dd($user);
})->middleware(['web']);

Laravel5.6 - override a vendor view

I try to make my own version of a vendor blade template.
I dont want to extends the controller with the reference of the view.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(__DIR__.'/../../resources/views/oauth/passport', 'passport');
I created a file named authorize.blade.php in /resources/views/oauth/passport
In the vendor controller method we can see this:
return $this->response->view('passport::authorize');
The problem is when I call the vendor controller method it loads his version of authorize.blade.php. I would like mine to be loaded and I expected the new line I added to AppServiceProvider to do that.
Passport comes with VUE components and views you need to publish first to override them. From the Laravel Passport page:
"If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command."
All you need to do is run php artisan vendor:publish --tag=passport-views and the vendor views will be place in resources/views/vendor/passport, where you can edit them.
Use can use php artisan vendor:publish --tag=passport-views this will copy the views to your views folder for you to change.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(DIR.'/../../resources/views/oauth/passport',
'passport');
You can use this option only by placing it in registry () instead of boot(). And then you can use your Views regardless of whether they were published in Vendor or not

Categories