Laravel event dispatch in web.php - laravel

I just wanted to use event broadcasting in Laravel and I followed a tutorial video. But problem shows up in the beginning. The tutorial shows what to do in web.php and it goes like this:
Route::get( uri: '/'. action: function () {
eventName::dispatch();
return view( view: 'welcome');
});
The problem is I have a different route formula in my web.php which is like this:
Route::get('{path}', SpaController::class)->where('path', '(.*)');
The question is, how to dispatch the event in my web.php?

Solve based on #ceejayoz's comment:
The tutorial is showing a quick, no-controller approach. You would call
eventName::dispatch(); within your SpaController.

Related

Laravel 9 Route problem return 404 NOT FOUND

I'm trying to render a view but I always get notfoud 404 I don't know how to solve this question anymore if anyone can help me I would be grateful.
any help is welcome.
Thanks in advance.
Everyone have a great Monday.
CommunityPostController -> code ->
public function creategif(Community $community)
{
return view('posts.creategif', compact('community'));
}
Web Routes
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('u/{id}', [App\Http\Controllers\HomeController::class, 'user'])->name('user.view');
Auth::routes(['verify' => true]);
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});
Views Files Structure
There might be a conflict base on your route order and the image you've posted. As you can see you have resource for your route which in this case this conflict might happen base on route order, and your laravel is actually trying to get a post instead of loading creategif route.
Which in this case every time you try to get access to creategif route, your application is actually trying to load a post base on 'communities.posts' route.
So base on this conflict in your route order, this is working fine and create or any other routes related to 'communities.posts' should work fine, but in other-hand creategif in URL might recognized as a route related to 'communities.posts' resource.
Move your route to top, or in this case just above 'communities.posts' route, don't forget to clear route cache.
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});

Laravel routes with vue/vue router

I'm basically using VueRouter to create all my routes which is working really well. However, my application is built using Laravel. So if I refresh any of the routes I get an error that the route is not defined. So at the minute in every controller I've had to add an index function. This just returns the view of my app.blade which is just the usual tags etc and the to load my single page app. I'm just wondering if there is a cleaner solution? I guess I could move the return view into a single Controller and make my Controllers extend this. But I'm just wondering if there is a better way I'm missing?
i.e. one of my routes via VueRouter:
{
path: "/clients",
name: "clients",
component: () => import(/* webpackChunkName: "clients" */ "../resources/js/components/Views/Clients/Clients.vue")
},
The route in my clients.php file
Route::get('/clients', [App\Http\Controllers\ClientController::class, 'index'])->name('clients');
Then the ClientController index function
public function index()
{
return view('app');
}
It would just be nice to have the loading of the app.blade done somewhere else and not need to be specified per controller. Any help would be appreciated to ensure it's all done efficiently!
Thanks!
Here is how I solved this issue for one of my projects which is also single page application in Vue and Laravel: https://github.com/lyyka/laravel-vue-blog-spa/blob/master/routes/web.php
Simply, in your routes file, you put this code:
Route::get('/{any}', function () {
return view('welcome');
})->where("any", ".*");
And in my welcome view I have:
#extends('layouts.app')
#section('content')
<div class = "container">
<router-view></router-view>
</div>
#endsection
Basically this will return your app view for any URL and so your Vue SPA should work properly. Generally, it is not a good practice to put callback functions inside your routes file, but in this case, you won't even be using your routes file as it is a SPA, so this solution can pass! :)
You should use your single html file and make a controller.
On your controller
public function index(){
return view('index');
}
on your web.php
basically, you should make the same route on your laravel and vue
Route::get('/products', [ProductsController::class,'index']);
in my vue-routes
import Products from './components/Products.vue'
{
path:'/products'
component: Products
}

Routing error in Laravel 5.3

I may be being thick but I am continually getting a page notfoundexception .
In a view I have the following:
<a href="{{ route('/galleryimages/{id}') }}"
This part is OK. In web.php I have:
Route::get('/galleryimages/{{id}}', function($id){
return view('gallery_pictures.pictures',['id'=>$id]);
});
The page pictures definitely exists in the subdirectory gallery_pictures.
Your route is incorrect, Laravel route parameters use one curly braces, like so: {id}. Rather than {{id}}
The helper function you are using accepts route names not route URL's, to link routes you should use the url() function
https://laravel.com/docs/5.3/helpers
https://laravel.com/docs/5.3/routing
I've provided you some for reference links if you haven't already checked them out.
HTML Fix:
My Link
Route Fix:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
});
A little extra
If you want to use the route function you must set a name for your route, you can do it as so:
Route::get('/galleryimages/{id}', function($id) {
return view('gallery_pictures.pictures', ['id'=> $id]);
})->name('gallery_images');
Then you may access the route by doing something like so
My Link

How to handle Laravel 4 sub-domain route with controller, passing subdomain as an argument

Following the Laravel 4 documentation on Routing, I've been trying to create a domain route that will handle a wildcard subdomain and pass it over to a controller action, but I'm having trouble passing the argument.
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', function($subdomain)
{
die($subdomain);
});
});
If I write the route like this, it will print out the subdomain, whatever it might be. The problem is that I don't want to write the code that handles these situations in the routes.php file, but use a Controller to handle it all, without redirecting from subdomain.myapp.com to myapp.com/controller/action/subdomain.
So, something like this:
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', 'MyController#myAction', $subdomain);
});
How do I pass the {subdomain} argument to the controller in this case?
It seems as though the morning is smarter than the night. I went with a dispatch solution, so if anyone else has a more elegant solution, please feel free to post and I'll accept your answer instead.
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', function($subdomain) {
$request = Request::create('/myRoute/' . $subdomain, 'GET', array());
return Route::dispatch($request)->getContent();
});
});
Route::get('myRoute/{subdomain}', 'MyController#myAction');

Laravel 4: Unable to generate a URL for the named route "login" as such route does not exist

I'm creating an authorization system in my Laravel 4 project. I am trying to use the auth "before" filter.
In my routes.php file, I have:
Route::get('viewer', array('before' => 'auth', function() {
return View::make('lead_viewer');
}));
Route::get('login', 'LoginController');
The before filter calls this line in the filters.php file:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
I can manually navigate to my login route. But the auth system isn't letting this happen. I've run composer dump-autoload a couple of times, so that isn't the problem. What am I doing, since I can actually load the login page if I do it manually?
I figure it out. Laravel is looking for a named route: I had to do this:
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}));
An interesting, not very intuitive approach in Laravel. But there must be a reason Taylor did this that I'm not seeing.
To do what you were trying to do in your initial approach you could have just done:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/login');
});
and it would have worked just fine.
If you want to use named routes then you do what you posted in your answer to your own question. Essentially...more than one way to skin a cat.
Hope that helps
I know you've probably solved this by now but after stumbling across your post while trying to solve a similar problem, I wanted to share my thoughts...
Laravel is NOT looking for a named route for the guest method, it is expecting a path.
Your example works because because the named route and the path are the same i.e. "login". Try changing your URL to something other than 'login' and watch it fail.
If you want to use a named route you should use the route helper method as so...
if (Auth::guest()) return Redirect::guest( route('login') )
Hope that helps someone.

Resources