how to create a blade template in laravel 5? - laravel

I have a challenge: I can't get a blade file working as expected following the Laravel guide for such a purpose.
I know a Laravel blade file must end with the .blade.php.
Every time I create a blade file, I get an error as the file cannot be found, but that is not the case with a pure PHP file.
Below is my route definition to return my blade test file, testblade.blade.php:
Route::get('/test', function () {
return view('testblade');
});
When returning a pure PHP file in my route file, testphpalone.php, I define my route as follow:
Route::get('/test', function () {
return view('testphpalone');
});
I'm concerned because I cannot use blade helper functions such as #yield() and #extends() directly in the pure PHP file, but a blade one (which I can't get working).
What am I doing wrong and how can I make it work?

Taking into consideration your route definition;
Route::get('/test', function () {
return view('testblade');
});
your blade template file should be testblade.blade.php and located in your resources/views directory as in: resources/views/testblade.blade.php for a proper implementation.

you only can use blade syntax in a blade file. As a best practice always name view files using .blade.php syntax.
find more on blade syntax here

In config\view.php check the path of the views
'paths' => [
realpath(base_path('resources/views')),
]

I had this problem and it was the server, I was using the XAMPP. How I found out is that I ran my project on an online server and it worked perfectly.
The Solution is just to run your project using the command:
php artisan serve
then browse your project using:
localhost:8000

Related

Route [add.sp] not defined

i wrote url like that
use App\Http\Controllers\DashController;
Route::get('/admin/special', [DashController::class, 'addSpecializations'])->name('add.sp');
this is controller
public function addSpecializations()
{
return view('dashboard.add-specializations');
}
when i tried to open it i can't even though all route work
after that i wrote this code in view's file
<a href="{{route('add.sp')}}">
so i faced this issue
Route [add.sp] not defined.
In case your routes are cached, run php artisan route:clear. In development, don't cache anything, including views and config.

Merging laravel dingo with laravel-modules and caching config

Here is the problem.
I started a dingo project and am using laravel-modules in it. Every module has its own routing files. Using the project in development environment, everything works fine.
But when I run php artisan config:cache, when a request comes to laravel, it return the response The version given was unknown or has no registered routes. As I see, the problem is dingo just check the default api.php and web.php files to find the route. But module routes are not stored in that files. I store them in Modules/module_name/route/api.php file (as laravel-modules suggested).
Any suggestion would be welcome.
change api file of module with version param in group session like this:
$api = app('Dingo\Api\Routing\Router');
$api->group(['version' => 'v1'], function ($api) {
...
});

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

Laravel 5 maintenance mode turn on without artisan

Is there any possibility to turn on and turn off Laravel 5 maintenance without php artisan up and down commands when my website is being hosted ?
What I've done:
Route::get('site/shutdown', function(){
return Artisan::call('down');
});
Route::get('site/live', function(){
return Artisan::call('up');
});
The first route is working fine. But when I call site/live the site still is shuted down. What can cause this problem ?
If your project is already down, you cannot call another function.
What happens after you run php artisan down is that it creates a file named down inside storage/framework. After running php artisan up the file is removed.
You can create the file manually inside storage/framework. It will down your project. When you want to take your project live again, just remove the file.
I think the right answer is missing here..
You could add your route to app/http/middleware/CheckForMaintenanceMode.php
protected $except = [
//here
];
So It never would be off.
when you run artisan down. site is not available so when try to call up, your IP can't access site.
you must call down with your IP exception.
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
or add ::1 to local.
to make that in route without command
try to save this command in specific one and call it.
Laravel 8 introduced secret in maintenance mode, in which you can bypass the maintenance mode by providing a secret, then your Artisan::call would work.
You could add your routes to the $except var in CheckForMaintenanceMode middleware to bypass the check. Then your site/live route would work just fine.
In order to make your site live again using an url, you can create a live.php file which you put in laravel's public folder and then visit http://your.domain/live.php .
In the live.php file you need something like this: (check your projects directory structure if you don't use the default public folder!)
<?php
unlink(dirname(__FILE__) . "/../storage/framework/down");
header("Location: your.domain");
die;
just put
Artisan::call('up');
without route function.

Routing doesn't work in laravel 4

I had installed Laravel with the following command
composer create-project laravel/laravel --prefer-dist
And everything is fine when I open / root. I saw "You have arrived." page but I can't make any route.When I write the following simple route I saw the page not found error.
Route::get('hello', function() {
return 'Hello World';
});
error:
Not Found
The requested URL /afifnet/public/hello was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80 please help me.
A couple of things. When you create a route you need to make sure you have a view. You can create a view by going the views folder (make sure you have the extension .blade.php
Create a View in your views folder (and add some content), eg:
test.blade.php
Then in your routes.php file add this:
Route::get('/test', function() {
return View::make('test');
});
Now goto public/test and you should see the content you added to test.blade.php file.
If you still cannot create a route it's another issue, however seeing as you can see the landing page "you have arrived" everything should be working.

Resources