How to access a view in a laravel module with routes? - laravel

I'm currently working on my homework for school which i've read about Laravel modules and i'm currently working with this https://nwidart.com/laravel-modules package.
I've managed to create modules, migrations and models. now i want to access a module via a simple link from route. like this:
CRM
I've created 3 modules with core,crm and sell names. which base on information from internet, I've understand that i can access them with localhost/crm or sell or core. now how i can fix my problem?
I've also tried {{ route('core::index') }}. Thanks
Update 1: Views in every module are like this: Resources\views\index.blade.php if u look at above link u will see a example of what i've said.
Update 2: routes: Every module have 1 route when i create module with package.
Laravel root module routes\web :
Route::get('/', function () {
return view('welcome');
});
and CRM route module Modules\CRM\routes\web\ and rest are just like below with different names :
Route::prefix('core')->group(function() {
Route::get('/', 'CoreController#index');
});

route() helper is for named routes. You can use url() helper to match a uri, or you have to add a name to your route, like:
Route::prefix('core')->group(function() {
Route::get('/', 'CoreController#index')->name('core.index');
});
and then you can
CRM

try to set name for your route
https://laravel.com/docs/5.8/routing#named-routes
Route::prefix('core')->group(function() {
Route::get('/', 'CoreController#index')->name('crm.index');
});
and then this should work
CRM

In the latest version 6 i change the view resource config and read from modules folder like this :
'paths' => [
base_path('Modules/Duet/Resources/views'),
base_path('Modules/anotherModule/Resources/views'),
],

I use like this to address view page in module controller:
return view("module::pages.main");
module is your module name.

Related

Laravel VueJS Router in backend

I'm working on a Laravel project where all frontend is managed by vuejs.
So, I have all backend routes defined on:
routes/api.php
And the file: routes/web.php it has this only route:
Route::view('{all}', 'app')
->where('all', '^((?!api).)*')
->name('vue');
Giving all the responsability to vue.js.
So inside resources/js (vue side) I have the frontend routes defined, for example:
{
path: '/',
name: 'home',
component: home,
meta: {
auth: true,
}
},
{
path: '/invoices/show/:id',
name: 'invoices.show',
component: InvoicesShow,
meta: {
auth: true,
}
},
Now the problem:
I started trying to create some notifications.
So on the notification I have to put the link where the customer has to go, but this link is to the frontend... So in Laravel I cannot write the vue.js route. Well, that's the question, is it possible to write the named route of vue.js?
I'd like to define this link on a notification for example:
->action('View Invoice', route('invoices.show', $invoiceId));
But the named route "invoices.show" it doesn't exist on Laravel routes. It's defined on vuejs router, in: resources/js/router/routes.js
So it won't work.
Is possible to get the vuejs router by name?
For now, I find impossible to do this.
So I simply write the full url "hardcoded".
->action('View Invoice', route('vue', ['all' => "invoices/show/{$invoiceId}"]));
This resolves my problem, but I cannot use the vue.js named routes.
One of the dirty workarounds would be to leave the VueJs route as it is and to create a new identical "helper" route for Laravel in your routes/web.php file.
So your web.php would look like this:
Route::view('/invoices/show/{id}', 'app')->name('invoices.show');
//Here you can add additional "helpers" routes as needed
Route::view('{all}', 'app')
->where('all', '^((?!api).)*')
->name('vue');
In this case, VueJs would still work fine and you would be able to use route() helper function in your project but a disadvantage is that you would need to maintain two routes if there is any changes.

Laravel route works with subdomain in 2 environments

I'm coding a platform to generate sub-websites.
I have a route like this who works very well in local :
//Website
Route::domain('{slug}.domain.test')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
I want to be able to make it works as well in production (other domain), so i did :
//Website
Route::domain('{slug}.{domain}')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
And in my template :
Website
The generated URL looks amazing, but the routing doesn't work and bring me to the parent page of the main domain.
What i am doing wrong ?
Thanks
Working with domain routes like this is a little bit of a pain in Laravel.
In an application recently, I parsed the domain part from the application URL and then set it as a configuration value like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([
'app.domain' => parse_url(config('app.url'), PHP_URL_HOST),
]);
}
}
You can then use that in your domain routes:
Route::domain('{slug}.'.config('app.domain'), function () {
// Subdomain routes that work in all environments
});
If you are using laravel homestead, you'll need to register every subdomain on the etc/hosts file and on Homestead.yaml, as you probably did with the main domain. Now, I'll recommend the structure:
app.test
subdomain1.app.test
subdomain2.app.test
I wouldn't recommend to use something like:
subdomain1.app1.test
subdomain2.app2.test
I mean, you could, but it doesn't make much sense, and you would also have to register all of this on your Homestead/Local and Production environments.
For the approach that I suggest, you could set up this on an environment variable. https://laravel.com/docs/6.x/configuration#environment-variable-types
You can add any env variable you want/need to the .env file, there's an APP_URL variable, but this includes the http protocol, you could add something like:
APP_DOMAIN=app.test
And for production
APP_DOMAIN=yourapp.com
Then on the routes file access it with the helper method env, you can omit the second parameter, or use it to setup a default value, in case you forget to put it on the .env file.
Route::domain('{slug}.' . env('APP_DOMAIN', 'default.com'))->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
BTW, this might be of help for setting up your slug value with named routes:
Setting up named routes within a subdomain group in Laravel 5.7

How to route to a controller method in Laravel Moduler development?

I am using "artem-schander/l5-modular": "dev-master" for laravel moduler development.
For example i create an Admin module.
Folder structure is App/Modules/Admin.
So controller related to Admin modules placed under App/Modules/Admin/Controllers/ directory.
All routes related to Admin module are placed in App/Modules/Admin/routes.php file.
Here how it looks
Route::group(array('module' => 'Admin', 'middleware' => ['web'],'namespace' => 'App\Modules\Admin\Controllers'), function() {
Route::resource('admin', 'AdminController');
});
All view files related to admin module placed in App/Modules/Admin/Views folder.
I am trying to access Admin's index view using this route
Route::get('/', 'AdminController#index');
This route is place in laravel default routes.php file.
and when i browse ,I am getting this error
Class App\Http\Controllers\AdminController does not exist
From this i understood , laravel looking AdminController in its default path.
How can i overcome this challenge ?
You can access a controller by full qualified namespace if it is not in default path.
Try:
Route::resource('admin', '\App\Modules\Admin\Controllers\AdminController');
I have found two way to do it.
First Option
Changing the $namespace in RouteServiceProvider.php .
For me
private $namespace='\App\Modules';
So for Admin module i can use route as
Route::get('/', 'Admin\Controllers\AdminController#index');
I think this is bad idea to change Laravel's default value.
Second Option
Providing full path of the Controller.
So route would be like this
Route::get('/','\App\Modules\Admin\Controllers\AdminController#index');

Custom Route for Laravel Nested Resource

I am making a Laravel app with url structure as follows
/users/{userid}/pets/{petid}
I want to make a custom route for
/users/{userid}/pets/seefavoritepets
But I can't figure out how to do it. I currently have:
Route::resource('users', 'UsersController');
Route::resource('users.pets', "PetsController");
But it can't figure out how to make the cutom route. I tried
Route::get('users.pets/seefavoritepets', function() {
return "getting favorite pets!";
});
to no avail. All help appreciated

How to associate a route to a package?

Can i do something like Symfony to associate a route to a package?
So every route on my routes.php admin package are prefixed with '/admin/'?
Because actually i can put a route on my main routes.php like '/admin' and i can put the same on my package routes without knowing that on my main routes this route is already in use.
My current package routes.php is something like this:
Route::get('/admin', 'MyVendor\administration\AdminController#test');
But what happens if i put the same '/admin' on the main routes? i dont like this approach.
Sorry for my English, i hope you understand me.
Thank you
Yes you can (from laravel site)
Route::group(array('prefix' => 'admin'), function()
{
Route::get('user', function()
{
//
});
});
Read the documentation about Route Prefixing and this answer too.

Resources