Custom Route for Laravel Nested Resource - laravel

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

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.

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.

Laravel Route, ->name method?

Laravel 5.5
which is the different doing (no get and post methods) on route defintion web.php file:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController#login');
regarding ->name('') method
Is required define that method? in which cases?
(sample taked from Auth Class definition laravel)
The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.
Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').
If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.

How can I make a dynamic route in Laravel 5.2

I need make a dynamic route or dynamic subdomain for multitenant site. I am not sure the better option. The matter is when the user create the account should get a URI like:
http://www.domain.dom/username
or
http://username.domain.com
I am not expert so I prefer the easiest way to do that. Any idea?
Thanks a lot
You can set up your routes like this:
// First example
Route::get('{username}', 'MyController#myAction');
// Second example
Route::group(['domain' => '{username}.domain.com'], function() {
Route::get('/', 'MyController#myAction');
});
Please note that you still have to set up your webserver to listen to all urls.
More info on subdomain routing can be found here: https://laravel.com/docs/5.2/routing#route-group-sub-domain-routing

Resources