How to associate a route to a package? - laravel

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.

Related

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.

Vue SPA url not working with history mode enabled

I am creating a SPA using Vue js and it kind of works but I have one problem. With history mode enabled on Vue I can enter urls and go to that page using the Vue Router but when I try to login I get the literal page html. I know I can do something like auth\{vue?} but I would prefer if i didn't have to do that. I want to be able to always keep the url with no prefixes unless it is to an API request. So for example:
I have the root view:
Route::get('/{vue?}', function () {
return view('layouts.app');
})->where('vue', '[\/\w\.-]*');
and then I have the api requests:
Route::group(['prefix' => 'api'], function () {
Route::post('/login', 'Auth\\LoginController#login');
Route::post('/register', 'Auth\\RegisterController#register');
Route::get('/logout', 'Auth\\LoginController#logout');
});
but when I hit /api/login I get the return view data from /{vue?}.
I hope that makes sense and if so any help would be amazing, Thanks.
it is clearly explained here: https://kjamesy.london/work/laravel-53-vuejs-20-make-vuerouters-history-mode-play-nicely-with-laravels-routes
Short story: declare history mode to false in VueRouter and then on the Laravel side, whenever the $request->ajax() is false, the same route will always capture the request. So, we can safely define a catch-all route to redirect such traffic to the index() method of our resource controller:

POST controller for all routes in Laravel 5

I have callback button in header of my webpage, so user can send me message from every page.
How to make route for this? Something like that:
Route::post('{*}', 'PostController#callback');
It would be better if you do it via ajax.
Use middleware so you can check every request for certain post-data.
I would do it like this: create a file called MessageMiddleware.php in the directory App\Http\Middleware\
<?php namespace App\Http\Middleware;
use Closure;
class MessageMiddleware {
public function handle($request, Closure $next) {
if(isset($_POST['internal_message'])) {
// Do something so the message reaches you (db, email, whatever)
}
return $next($request);
}
}
?>
This is just a very basic version but should give you an idea.
You will not have to register any routes for this and the middleware will work for all urls the middleware is registered for.
If you want a middleware to be run during every HTTP request to your application, simply list the middleware class App\Http\Middlware\MessageMiddleware in the $middleware property of your app/Http/Kernel.php class.
The official Laravel documentation for Middlware is very extensive and does certainly not only cover authentication middleware.
Doing some try and check I found that the most simple route allows sending callback request from every page!!!
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::post('/', ['as' => 'callback', 'uses' => 'PostController#callback']);
But I don't know why. If someone know why please tell me, because I really want to know the background.
I was also trying to do it using middleware as I was advised. It was also working solution. Messages were sending from every page, but with message I got 403 error code in console every time. And of course I was trying to get rid of that.
With this simple solution it works without any errors in console.

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

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');

Resources