Site refresh time is long in laravel? - laravel

Hello and do not be tired!!
My admin panel pages are hard to load...It takes 15 seconds for the pages to load!!
The problem is with my routes!!!
Because when I define the path as follows, the pages load quickly :
Route::get('users/index', function () {
return view('admin.index');
});
But the route I have defined is as follows :
Route::group(['middleware' => ['auth' , 'InfoFolder' , 'verified' , 'Roles'] , 'prefix' => 'users/'] , function(){
Route::get('{url}', [UrlController::class , 'urlpanel'])->name('users_url');
});
And the control I have defined :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UrlController extends Controller
{
//
public function urlpanel($url ){
$admin = "admin";
$pages = "admin.pages";
$charts = "admin.pages.charts";
$examples = "admin.pages.examples";
$forms = "admin.pages.forms";
$mailbox = "admin.pages.mailbox";
$tables = "admin.pages.tables";
$UI = "admin.pages.UI";
$link_panel = [
'index' => "$admin.index",
'list_users' => "$admin.list_users",
'index3' => "$admin.index3",
'calendar' => "$pages.calendar",
'widgets' => "$pages.widgets",
'chartjs' => "$charts.chartjs",
'flot' => "$charts.flot",
'inline' => "$charts.inline",
'404' => "$examples.404",
'500' => "$examples.500",
'blank' => "$examples.blank",
'invoice-print' => "$examples.invoice-print",
'invoice' => "$examples.invoice",
'lockscreen' => "$examples.lockscreen",
'login' => "$examples.login",
'profile' => "$examples.profile",
'register' => "$examples.register",
'product_add' => "$forms.product_add",
'editors' => "$forms.editors",
'general' => "$forms.general",
'compose' => "$mailbox.compose",
'mailbox' => "$mailbox.mailbox",
'read-mail' => "$mailbox.read-mail",
'data' => "$tables.data",
'simple' => "$tables.simple",
'buttons' => "$UI.buttons",
'general' => "$UI.general",
'icons' => "$UI.icons",
'sliders' => "$UI.sliders",
];
if(!in_array($link_panel[$url] , $link_panel)){
return abort(404);
}
return view($link_panel[$url]);
}
}
How can I increase the loading speed of my website?
The project has been uploaded to Localhost
:)

Try to check the middlewares you are using. Maybe in one of the middlewares there is some time consuming action.

Related

Preview image in ACF gutenberg block

Is it possible to add an image to the gutenber block preview when using ACF to register the block ?
Here's the code to register the block:
acf_register_block(array(
'name' => 'bk-raisons',
'title' => __('Les raisons', 'diezel'),
'description' => __('Les raisons', 'diezel'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'spira-custom',
'icon' => 'align-wide',
'keywords' => array('bk-raisons'),
));
The preview appears when hovering the block.
Thank you !
I finally found a solution.
I don't know if you use Twig (Timber) or not.
If not check this : https://stackoverflow.com/a/67846162/6696150
For my part with Timber
When you declared your block add example attributes :
$img_quadruple = array(
'name' => 'img-quadruple',
'title' => __('Quatre images'),
'title_for_render' => 'img-quadruple',
'description' => __(''),
'render_callback' => 'ccn_acf_block_render_callback',
'category' => 'imgs',
'icon' => '',
'mode' => 'edit',
'keywords' => array( 'quatre images' ),
'example' => array(
'attributes' => array(
'mode' => 'preview',
'data' => array(
'preview_image_help' => get_template_directory_uri().'/assets/img/preview/preview_img_quadruple.jpg',
),
)
)
);
And when your declared your callback :
function ccn_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
$context = Timber::context();
// Store block values.
$context['block'] = $block;
// Store field values.
$context['fields'] = get_fields();
// back-end previews
if ( $is_preview && ! empty( $block['data'] ) ) {
echo '<img src="'. $block['data']['preview_image_help'] .'" style="width:100%; height:auto;">';
return;
} elseif ( $is_preview ) {
echo 'Other condition';
return;
}
// Store $is_preview value.
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render('gutenberg/gut_' . strtolower($block['title_for_render']) . '.twig', $context );
}

laravel save pdf no such file or directory

So I want to save a pdf file to a directory on my local server but it keeps saying that the directory does not exist.
So first of all where would you store PDF files that are not accessible to by externals (so not in the public folder).
So this is my code. The download works perfectly.
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$path = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save('storage/app/public/'.$path.'.pdf');
;
return $pdf->download(''.$path.'.pdf');
}
First of all, you should check if the directory exists with File facade. If it does not exist, you must make the directory.
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path);
}
If the error still occurs, you must force it to make the directory:
if(!File::exists($directory_path)) {
File::makeDirectory($directory_path, $mode = 0755, true, true);
}
After that, you can save the file in that directory.
Second, if you don't want to save the file in the public directory. you must save it in storage.By simply call storage_path($file_path). this way laravel saves the file under storage/app/public directory.
after that, you can get the URL of the file according to this answer.
I figured it out thank you for your answer.
This is my code:
public function generatePDF()
{
$this->mailorder = Session::get('order');
$this->cart = Session::get('cart');
$data = [
'id' => $this->mailorder->id,
'client' => $this->mailorder->Contact,
'country' => $this->mailorder->country,
'city' => $this->mailorder->city,
'street' => $this->mailorder->street,
'postal' => $this->mailorder->postal,
'phone' => $this->mailorder->phone,
'email' => $this->mailorder->email,
'dateIn' => $this->mailorder->dateIn,
'dateOut' => $this->mailorder->dateOut,
'subtotal' => $this->mailorder->subtotal,
'tax' => $this->mailorder->tax,
'total' => $this->mailorder->total,
'cart' => $this->mailorder->cart,
'delivery' => $this->mailorder->delivery,
];
$filename = "order_{$this->mailorder->id}_{$this->mailorder->Contact}";
$path = storage_path('pdf/orders');
if(!File::exists($path)) {
File::makeDirectory($path, $mode = 0755, true, true);
}
else {}
$pdf = PDF::loadView('pdf.orderConfirmationPdf', $data)->save(''.$path.'/'.$filename.'.pdf');
;
return $pdf->download(''.$filename.'.pdf');
}

Insert multiple data from other table in codeigniter

I want to save the data where id_perencanaan is selected. I've tried a lot of ways, but have not found the answer.
Controller:
public function salin_barang_perencanaan($id_perencanaan) {
$barang_perencanaan = $this->perencanaan_barang_model->barang_perencanaan($id_perencanaan);
// echo "<pre>";
// print_r($barang_perencanaan);
if($barang_perencanaan->id_perencanaan == 0) {
$data = array(
'id_perencanaan_barang' => $barang_perencanaan->id_perencanaan_barang,
'id_golongan_barang' => $barang_perencanaan->id_golongan_barang,
'id_bidang_barang' => $barang_perencanaan->id_bidang_barang,
'id_kelompok_barang' => $barang_perencanaan->id_kelompok_barang,
'id_sub_kelompok_barang' => $barang_perencanaan->id_sub_kelompok_barang,
'id_jenis_barang' => $barang_perencanaan->id_jenis_barang,
'id_perencanaan' => $barang_perencanaan->id_perencanaan,
'nomor_barang' => $barang_perencanaan->nomor_barang,
'nama_barang' => $barang_perencanaan->nama_barang,
'harga_satuan' => $barang_perencanaan->harga_satuan,
'jumlah_barang' => $barang_perencanaan->jumlah_barang,
'total_harga' => $barang_perencanaan->total_harga,
'penggunaan_barang' => $barang_perencanaan->penggunaan_barang,
'keterangan' => $barang_perencanaan->keterangan,
'tanggal_post' => date('Y-m-d H:i:s'),
'id_user' => $this->session->userdata('id')
);
$this->perencanaan_model->salin_barang_perencanaan($data);
$this->session->set_flashdata('sukses', 'Perencanaan dalam tahap pengadaan');
redirect(base_url('pengadaan'));
}
$this->session->set_flashdata('sukses', 'Proses perencanaan telah dibatalkan');
redirect(base_url('perencanaan'));
}
And my model :
public function salin_barang_perencanaan($data) {
// $this->db->trans_start();
$this->db->where('id_perencanaan',$data['id_perencanaan']);
$this->db->insert_batch('pengadaan_barang',$data);
// $this->db->trans_complete();
}
I am very grateful for your help...
The problem has been resolved, the following is the code I used:
Controller :
public function salin_barang_perencanaan($id_perencanaan) {
$barang_perencanaan = $this->perencanaan_barang_model->barang_perencanaan($id_perencanaan);
// echo "<pre>";
// print_r($barang_perencanaan);
// if($barang_perencanaan->id_perencanaan == 0) {
foreach($barang_perencanaan as $barang_perencanaan){
$data = array(
'id_perencanaan_barang' => $barang_perencanaan['id_perencanaan_barang'],
'id_golongan_barang' => $barang_perencanaan['id_golongan_barang'],
'id_bidang_barang' => $barang_perencanaan['id_bidang_barang'],
'id_kelompok_barang' => $barang_perencanaan['id_kelompok_barang'],
'id_sub_kelompok_barang' => $barang_perencanaan['id_sub_kelompok_barang'],
'id_jenis_barang' => $barang_perencanaan['id_jenis_barang'],
'id_perencanaan' => $barang_perencanaan['id_perencanaan'],
// 'id_pengadaan' => $last_id,
'nomor_barang' => $barang_perencanaan['nomor_barang'],
'nama_barang' => $barang_perencanaan['nama_barang'],
'harga_satuan' => $barang_perencanaan['harga_satuan'],
'jumlah_barang' => $barang_perencanaan['jumlah_barang'],
'total_harga' => $barang_perencanaan['total_harga'],
'penggunaan_barang' => $barang_perencanaan['penggunaan_barang'],
'keterangan' => $barang_perencanaan['keterangan'],
'tanggal_post' => date('Y-m-d H:i:s'),
'id_user' => $barang_perencanaan['id_user']
);
$this->perencanaan_model->salin_barang_perencanaan($data);
}
$this->session->set_flashdata('sukses', 'Perencanaan dalam tahap pengadaan');
redirect(base_url('pengadaan'));
// }
// $this->session->set_flashdata('sukses', 'Proses perencanaan telah dibatalkan');
// redirect(base_url('perencanaan'));
}
And my model :
public function salin_barang_perencanaan($data) {
$this->db->trans_start();
$this->db->insert('pengadaan_barang' ,$data ,array('id_perencanaan' => $data['id_perencanaan']));
$this->db->trans_complete();
}

authorisation based on values not on verb

I'm implementing an authorisation process based on values not on verb.
Example:
all users/groups are allowed to update a db field
admin can set status to all possible values (confirmed | pending | cancelled)
supplier can set to 'confirmed'
All examples I find go around a user/group being able or not to insert,update or delete something.
Is there something out there to deal with situations like this out of the box, or this has be hard coded?
Basically what you need is something hard to maintain in any application: granular permissions.
You can get this to work easily using Cartalyst's Sentry. Here's how I do it:
All my routes are hierarchically organized and named as such:
<?php
Route::get('login', array('as'=>'logon.login', 'uses'=>'LogonController#login'));
Route::get('logged/out', array('as'=>'logon.loggedOut', 'uses'=>'LogonController#loggedOut'));
Route::group(array('before' => 'auth'), function()
{
Route::get('logout', array('as'=>'logon.logout', 'uses'=>'LogonController#logout'));
Route::group(array('before' => 'permissions'), function()
{
Route::get('store/checkout/shipping/address', array('as'=>'store.checkout.shipping.address', 'uses'=>'StoreController#shippingAddress'));
Route::get('store/checkout/payment/confirmed', array('as'=>'store.checkout.payment.confirmed', 'uses'=>'StoreController#confirmed'));
Route::get('profile', array('as'=>'profile.show', 'uses'=>'ProfileController#show'));
});
});
Those under the filter 'permissions' are subject to check if user has rights to use them:
Route::filter('permissions', function()
{
$name = Route::current()->getName();
$name = 'system' . ( ! empty($name) ? '.' : '') . $name;
if (!Permission::has($name)) {
App::abort(401, 'You are not authorized to access route '.$name);
}
});
Basically here I get the current route name, add 'system.' to it and check if the user has this particular permission.
Here's how I create my groups and populate permissions:
<?php
public function seedPermissions()
{
DB::table('groups')->truncate();
$id = 1;
Sentry::getGroupProvider()->create(array(
'id' => $id++,
'name' => 'Super Administrators',
'permissions' => array(
'system' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'id' => $id++,
'name' => 'Administrators',
'permissions' => array(
'system.users' => 1,
'system.products' => 1,
'system.store' => 1,
'system.profile' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'id' => $id++,
'name' => 'Managers',
'permissions' => array(
'system.products' => 1,
'system.store' => 1,
'system.profile' => 1,
),
));
Sentry::getGroupProvider()->create(array(
'id' => $id++,
'name' => 'Users',
'permissions' => array(
'system.store.checkout' => 1,
'system.profile' => 1,
),
));
}
So if a user is trying to add some shipping address, the route 'store.checkout.payment.confirmed', as every user has access to 'system.store.checkout', everything inside that route will available to him.
And this is how I check for permissions:
public static function has($permission)
{
$all = [];
$parts = explode('.',$permission);
$permission = '';
foreach($parts as $part) {
$permission .= (!empty($permission) ? '.' : '') . $part;
$all[] = $permission;
}
return Sentry::check() and Sentry::getUser()->hasAnyAccess($all);
}
It basically builds a list of routes:
system
system.store
system.store.checkout
system.store.checkout.payment
system.store.checkout.payment.confirmed
And if Sentry finds one of those in the users granular permissions it will return true.
Now I just have to add users to groups:
Sentry::findUserById(1)->addGroup( Sentry::getGroupProvider()->findByName('Users') );
And if I need to go granular on a particular user/permission I just need to:
$user = Sentry::findUserById(1);
$user->permissions['store.checkout.payment'] = false;
$user->save();
And the user will never be able to pay for anything in the store again. :)

Adding class to form_dropdown in CodeIgniter

How do I add a class to a form_dropdown in CodeIgniter if I also have a Javascript attached. Here is my code:
$language = array(
'select' => 'Select Language',
'chinese' => '中文',
'english' => 'English',
'french' => 'Français',
'german' => 'Deutsch',
'italian' => 'Italiano',
'japanese' => '日本',
'korean' => '한국어',
'polish' => 'Polska',
'portuguese' => 'Português',
'russian' => 'Росс&#1048Ю',
'spanish' => 'Español'
);
$js = 'onChange="this.form.submit()"';
echo form_dropdown('language', $language, 'select', $js);
Add the class to the $js variable;
$js = 'onChange="this.form.submit()" class="myClass"';

Resources