It is not possible to completely prevent someone from viewing the source code of a web page, as it is ultimately downloaded and rendered on the client-side. However, there are some measures you can take to make it more difficult for someone to view the source code.
One approach is to obfuscate the code, which involves transforming the code into a form that is difficult for humans to read and understand. This can be done using tools like JavaScript obfuscators or minifiers.
Another approach is to use server-side rendering, which involves generating the HTML on the server and sending it to the client as a pre-rendered page. This can make it more difficult for someone to view the source code, as they would need to intercept the network traffic to see the HTML.
However, both of these approaches have limitations and trade-offs, and ultimately cannot completely prevent someone from viewing the source code if they are determined to do so.
// Example of using server-side rendering with Laravel
Route::get('/', function () {
$data = [
'title' => 'My Page',
'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
];
return view('my-page', $data);
});
// my-page.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{ $title }}</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>{{ $content }}</p>
</body>
</html>