I am following a tutorial to learn Laravel 8. I've created a simple view that contains 3 articles. Each article has a link to another html page. The exercise is to create URLs in web.php.ur
I have 2 views post.blade.php and posts.blade.php(the main one) , the other articles in html are contained in a posts folder in ressources.
Route::get('posts/{post}', function ($view) {
$post = file_get_contents(__DIR__ . "/../ressources/posts/{$view}.html");
return view('post', [
'post' => $post,
]);
});
This is the url: http://127.0.0.1:8000/posts/my-first-post
This the error I get:
file_get_contents(C:\xampp\htdocs\laravel2\laravel2\routes/../ressources/posts/my-first-post.html): Failed to open stream: No such file or directory
I don't understand what went wrong since I'm copying exactly what the tutorial is doing.
Start from here https://laracasts.com/series/laravel-8-from-scratch
You will have each nuts and bolts of laravel clear. This series is very precise to get you started in laravel.
In routes/web.php page
Route::get('/posts', [PostController::class, 'index']);
And in View page
Posts
First, i want to say that if your instructor really told you to write this
file_get_contents(__DIR__ . "/../ressources/posts/{$view}.html") , then he doesn't know anything about laravel and the MVC structure. stop following that.
This is the solution based on what i understood from your example:
so there is a html file named 'my-first-post.html' in your 'ressources/posts' directory. Rename it to 'my-first-post.blade.php'
in the post.blade.php file, just include that file using #include('posts.' . $post)
and change the controller to this:
Route::get('posts/{post}', function ($post) {
return view('post', [
'post' => $post,
]);
});
Related
I am actually following up on a Laravel course that uses Blade component template and the tutor coded the Login/Register/Dashboard, etc individually. I couldn't keep up with the coding and I sorted for a quicker way of adding those features.
That was how I discovered I can do those things with Jetstream. I installed Jetstream/Livewire and the above mentioned features were added automatically with a default view/welcome.blade.php connected to Route/web.php.
Now here is my problem. Because I still want to follow through with the course by using Blade template method, I deleted the defaulted welcome.blade.php(as the tutor did too), created two folders with a file respectively: Layouts/app.blade.php and Posts/index.blade.php.
On the Route/web.php, I changed the default welcome to post.index but it is not working. Here is the code on my Route:
Route::get('/post', function () {
return Inertia::render('post.index', [
'canLogin' => Route::has('login'),
'canRegister' => Route::has('register'),
'laravelVersion' => Application::VERSION,
'phpVersion' => PHP_VERSION,
]);
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return Inertia::render('Dashboard');
})->name('dashboard');
And here is the error I got:
InvalidArgumentException
View [app] not found.
http://127.0.0.1:8000/post
app was not found.
Are you sure the view exists and is a .blade.php file?
This is the screenshot of my editor to show the code and prove I have the view files.
Edited Here are the index.blade.php and app.blade.php as requested by #Prospero:
Is there a nice way, to solve this issue: I have a folder ressources/views/project/content with several blade teplates, let's say:
home.blade.php
how-to.blade.php
info.blade.php
best-way-to-score.blade.php
...
Right now, I define one view route per file:
Route::view('/home', 'project.content.home')->name('home');
Route::view('/how-to', 'project.content.how-to')->name('how-to');
...
How can I create these routes on thy fly? I could solve it with a loop through all files in this directory, but maybe there is a more elegant way/function in laravel I don't know yet?
If I understand correctly, what you you need is a generic get route like this:
Route::get('/{page}', 'PageController#show');
and then you need a PageController with a function to return the requested page:
public function show($page)
{
return view('project.content.'.$page);
}
Just have in mind that this kind of route will "catch" every get request so put it at the end of the web.php file
I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/
I am new to Laravel and was following a tutorial (https://www.youtube.com/watch?v=Zz_R73eW3OU&list=PL09BB956FCFB5C5FD). I have just installed the latest version of Laravel and am going through the beginning stages of the tutorial. So far (installation, creating a migration) I was doing ok, but now I am stuck at the point of creating new routes.
I have created a controller in app/controllers:
<?php
class Slots_Controller extends Base_Controller{
public $restful = true;
public function get_index(){
return View::make( 'slots.index' );
}
}
As well as a very minimalist view in the file app/views/slots/index.php
<h1>Slots</h1>
My app/routes file looks like this :
Route::get('/', function()
{
return View::make('hello');
});
Route::get( '/public/slots', array(
'uses' => 'slots#index'
) );
I've searched a number of things on the web and here is how far I have come :
- i know ideally only the public dir should be in the htdocs, i will be doing that presumably at a further stage of the tutorial. so as of now, my laravel home page is at http://localhost:8888/my_directory.laravel/public/
- i have checked the apache mod-rewrite : it is on and it works well for codeIgniter
- i have checked that the .htaccess is as provided by laravel (and as suggested by different posts)
The weird thing is, if i change the routes for the home page like this :
Route::get('/', function()
{
return 'hello world';
// return View::make('hello');
});
Nothing changes, i.e. i still get the original welcome page. My understanding is this modification should have simply output "hello world" as the html for the home page.
So it seems that whatever I'm doing in routes.php has no effect whatsoever, as if I was modifying the wrong file or something.
I've spent the most part of an otherwise sunny afternoon on this, any help is much apreciated :)
Thanks,
Tepp
There are many things you are doing wrong here (The best way is to follow the tutorial on Laravel):
In your controller file you are mentioning "Base_Controller", are you sure you have such a file in your controllers directory? (According to my knowledge it should be BaseController)
The Route::get is not defined properly. There are many ways to define a route; however for your case it should be something like this:
Route::get('slots', array(
'uses' => 'Slots_Controller#get_index'
));
Hope this helps.
I'm learning Laravel, and for my first project I'd like to create my portfolio. However, the first task I have to do is confusing me.
So I created my templates, layout.blade.php and home.blade.php. That makes sense to me, but now how do I tell Laravel, or how do I route to home.blade.php?
I'm looking for an explanation rather then just code. I'm trying to learn.
Actually, a view in MVC application is just a part of the application and it's only for presentation logic, the UI and one doesn't call/load a view directly without the help of another part (controller/function) of the application. Basically, you make a request to a route and that route passes the control over to a controller/function and from there you show/load the view. So it's not a tutorial site and it's also not possible to explain about MVC here, you should read about it and for Laravel, it's best place to understand the basics on it's documentation, well explained with examples, anyways.
In case of Laravel, you should create a controller/class or an anonymous function in your apps/routes.php file and show the view from one of those. Just follow the given instruction step by step.
Using a Class:
To create a route to your Home Controller you should add this code in your app/routes.php
// This will call "showWelcome" method in your "HomeController" class
Route::any('/', array( 'as' => 'home', 'uses' => 'HomeController#showWelcome' ));
Then create the HomeController controller/class (create a file in your controllers folder and save this file using HomeController.php as it's name) then paste the code given below
class HomeController extends BaseController {
public function showWelcome()
{
// whatever you do, do it here
// prepare some data to use in the view (optional)
$data['page_title'] = 'Home Page';
// finally load the view
return View::make('home', $data);
}
}
If you have {{ $title }} in your home.blade.php then it'll print Home Page. So, to use a view you need a controller or an anonymous function and load the view from the controller/function.
Using an anonymous function:
Also, you can use an anonymous function instead of a controller/class to show the view from directly your route, i.e.
Route::any('/', function(){
// return View::make('home');
// or this
$data['page_title'] = 'Home Page'; // (optional)
return View::make('home', $data);
});
Using this approach, whenever you make a request to the home page, Laravel will call the anonymous function given in/as route's callback and from there you show your view.
Make sure to extend the the master/main layout in sub view (home):
Also, remember that, you have following at the first line of your home.blade.php file
#extends('layouts.layout')
It looks confusing, you may rename the main layout (layout.blade.php) to master.blade.php and use following in your home.blade.php instead
#extends('layouts.master')
Read the doc/understand basics:
You should read Laravel's documentation properly, (check templates to understand blade templating) and also read some MVC examples, that may help you too understand the basics of an MVC framework (you may find more by googling) and some good posts about MVC on SO.
Check it routing in Laravel.
You need to use route file and controllers
Create needed function in your Controller file and create a template file for example
class UserController extends BaseController {
/**
* Show the profile for the given user.
*/
public function showProfile($id)
{
$user = User::find($id);
return View::make('user.profile', array('user' => $user));
}
}
you need to create view file views/user/profile.blade.php
View::make('user.profile', array('user' => $user)) == views/user/profile.blade.php
And you should read it http://laravel.com/docs/responses and also this http://laravel.com/docs/quick#creating-a-view