how to navigate between components in Laravel using dynamic component - laravel

I'm working on a little project using Laravel and VueJs, I'm also using dynamic component in my blade template.
I would like to know home can I navigate between pages(components) since I'm not using vue-router.
this is my code :
methods: {
register(url){
Csrf.getCookie().then(() => {
User.register(url, this.form)
.then(() => {
// how can i redirect the user since i'm not using vue router / should i use vue router ?
})
.catch(error => {
});
})
}
}

Your choices are pretty much either:
Use vue-router, including its element on a single Blade view and then navigating between Vue routes with <router-link> or this.$router.push()
Use a single Vue component per Blade view in Laravel, navigating between them with a standard <a href> link

Related

Laravel + Vuejs: How to create a link to a blade template from Vuejs Component

I'm trying to keep Login and Register in blade. I used the default php artisan ui vue --auth.
Then keep the SPA in Vuejs with Vuetify.
App.vue
...sidebar containing link to register
<v-list-item link #click="register">
<v-list-item-action><v-icon>mdi-icon</v-icon></v-list-item-action>
<v-list-item-content><v-list-item-title>Register</v-list-item-title></v-list-item-content>
</v-list-item>
...
<script>
methods: {
register() {
axios.post('/register')
.finally( err => {
window.location = '/register';
})
},
}
</script>
If you click the register link, it keeps going to "/home" instead of "/register". Help please thanks.
instead of
window.location = '/register';
use this
window.location.href = '/register';

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
}

how to pass laravel trans strings to vue component

In the event that you are developing your software using the Laravel framework and need to use the Vue library, you will face some obstacles, including how to access the files translation system provided by Laravel .
like how to access trans('home.var')
Add Language file like this
<messagings v-bind:language="{{ json_encode(trans('messages')) }}"></messagings>
then in vue component print like this
<h4>{{ this.language.messaging }}</h4>
1: pass trans as prop in blade normally just you need to encoding with json used json_encode() method :
<component :trans="{{json_encode(trans('home'))}}"></component>
2: in vue component page you need to decode json data first and set the translation data in some var :
export default {
props:['trans'],
data () {
return {
translation:[],
};
},
created(){
this.setTranslation()
},
methods:{
setTranslation(){
let decoded_trans = JSON.parse(this.trans);
this.transaction = decoded_trans;
}
}
3: Now you can access laravel trans normaly just be careful because vue will not detect if you call last node or first.
<template>
<div>
<h2>{{translation.header.login}}</h2> // this line in laravel mean : trans('home.header.login')
</div>
<template>
Finally I recently started learning vue , so this method may have some flaws, but We all strive to benefit :)

How to make redirection to some vue form from laravel's control?

In my Laravel 5.6 application with vue.js 2.5.7 I and vue-router 3
I use Socialite for login into the system
and in case of success I need to make redirection to some vue form with success message and next options available.
For this I make redirection like:
$url= $site_hosting . "/home";
return redirect()->route($url, [])->with([
'text' => 'google_new_user',
'type' => 'success',
'action' => 'successful_login'
]);
where url has value like
example.com/home
the question is that I do not know how redirecting from Laravel control to point to Vue component, defined in resources/assets/js/app.js file as
const routes = [
{
components: {
notFound: NotFound,
...
How can it be done ?
MODIFIED BLOCK # 2
Thank you for the link, but actually I did not catch the decision.
I see in laravel's route file url with "vue_capture" defintion,
but in url examples there are
/resource/,
/posts/,
/posts/{slug}
and “storage” in reg expression. What are "storage"/"vue_capture" some predefined names for some actions ?
Could you, please, give some detailed explanations how it would work, as I need FROM lasravel's action to open vue form?
Also I starting working with vue reading some docs/videos and now my router is defined as :
const router = new VueRouter( {
mode: 'hash', // default
routes
})
with a lot of routes defined.
I mean mode has different value here. Will this example work with this option ?
Thanks!
Check out vue router. https://router.vuejs.org/
Here's an in depth tutorial https://laravel-news.com/using-vue-router-laravel

how to add permission control in laravel with vue project?

i'm adding vue js into my current laravel apps so i can make it SPA and thats mean laravel is only acting as backend and rest of it is controlled by vue js from rendering view to routing.
My current laravel apps using kodeine laravel-acl for controlling which page and which action that my user can do by simply
put this in route:
Route::get('admin', [
'as' => 'admins.admin.index',
'uses' => 'UserController#index',
'middleware' => ['auth', 'acl'],
'can' => 'view.admin_view']);
and if i just do this in view to hide some action if user don't have permission
#permission('view.admin_view')
//some link or button
#endpermission
and all these permission is stored in mysql database and each user have different set of permission.
but how to do that if i making it vue js SPA? since all route is controlled via vue js?
You can perform checks before going to a certain route via beforeEnter() in routeConfig. Sample is here:
const router = new VueRouter({
routes: [
{
path: '/foo',
component: Foo,
beforeEnter: (to, from, next) => {
// ... Do your checkings here
}
}
]
})
To simply permission detection logic you can use CASL - https://github.com/stalniy/casl
There are a lot of interesting stuff in documentation and articles on medium explaining how integrate with Vue and other popular frameworks!

Resources