Laravel VueJS Router in backend - laravel

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.

Related

Merging laravel dingo with laravel-modules and caching config

Here is the problem.
I started a dingo project and am using laravel-modules in it. Every module has its own routing files. Using the project in development environment, everything works fine.
But when I run php artisan config:cache, when a request comes to laravel, it return the response The version given was unknown or has no registered routes. As I see, the problem is dingo just check the default api.php and web.php files to find the route. But module routes are not stored in that files. I store them in Modules/module_name/route/api.php file (as laravel-modules suggested).
Any suggestion would be welcome.
change api file of module with version param in group session like this:
$api = app('Dingo\Api\Routing\Router');
$api->group(['version' => 'v1'], function ($api) {
...
});

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

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

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.

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

Resources