I'm trying to experiment in symfony3. I'm trying to search it to google but I found no clear answer from them. This is the default controller in symfony
class DefaultController extends Controller
{
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
]);
}
}
If you see the return render. It will display the default/index.html.twig from the app/Resources/views/default/index.html.twig I would like to know if this possible to put my view folder in my AppBundle folder? here is my list folder in AppBundle
AppBundle
-Controller
-Entity
-Form
-Resources
-views
-default
-index.html.twig
How can I link it to the resources under my AppBundle? If you have tutorial please give link. thanks in advance
Symfony2 started with views under the AppBundle/Resources, and Symfony3 try to encourage you to separate the frontend (html/js/css) from your Business php Bundle by placing them in app/Resources.
Anyway if you want to structure the code as you describe it (like symfony2 standard), the syntax to use is bundle:folder:subdirs/views.html.twig
AppBundle // first parameter
-Controller
-Entity
-Form
-Resources
-views
-default //second parameter
- index.html.twig
- subfolder
- _included.html.twig
- subsubfolder
- _very_included.html.twig
In this example:
index.html.twig the syntax will be AppBundle:default:index.html.twig
_included.html.twig the syntax will be AppBundle:default:subfolder/_included.html.twig
_very_included.html.twig the syntax will be AppBundle:default:subfolder/subsubfolder/_very_included.html.twig
For your concrete problem it will be AppBundle:default:index.html.twig
Related
I am using Symfony 5.0.4 with easyadmin by following this tutorial.
https://symfony.com/doc/master/bundles/EasyAdminBundle/index.html
After installing easyadmin, I tried to get my homepage https://127.0.0.1:8000/.
But it redirects to /admin.
All config files are set by default.
How can I load the homepage without being redirected?
Thank you so much, Alexandre!
The problem was on the global route. I don't know why.
Example:
/**
* #Route("/", name="labs_")
*/
class IndexController extends AbstractController
{
// ...
}
When I moved route inside everything homepage started working.
You should update the config\packages\easy_admin.yaml file and add a link to homepage into the menu:
easy_admin:
design:
menu:
- { route: 'homepage', label: 'Back to the website', icon: 'home'}
Do not forget to replace 'homepage' by the route name of your route page.
You can find the route to your homepage by type:
symfony console debug:route
PS: If the debug:route command is unknown, you should install debug-pack:
symfony composer req debug --dev
Then you have to search in your code the controller corresponding to the homepage route.
Here is an example:
class DefaultController extends AbstractController
{
/**
* #Route("/", name="homepage") <== This is the method called when I wand to display the homepage
*/
public function index(): Response
{
return new Response('This is an example');
}
}
You should find the method called in your application and verify its code to find why it redirects you to the easyadmin route.
I have created a module in Laravel and im using views on that module, my structure is this:
Modules
-> MyModule
->->Controllers
->->Views
->->->MyModule.blade.php
But i have the headers and footer done on resources->views->layouts->base.blade.php
So how can i call this one so i can use the same base layout in all modules? it is possible on Laravel 5?
Already tried this
#include('layouts.base')
but im getting
Trying to get property of non-object (View: ... resources\views\layouts\base.blade.php
Thank you.
The structure of blade is relative to the views folder in the the resources folder.
Thus making your #include() have a structure like the this:
#include('DIRECTORY.BLADE')
and you can include your various blade contents by using #yield()
#yield('YIELD_FIELD_NAME')
If you are trying to have blades extent from that layout you would call that at the top of the blade files you want to extend off it.
#extends('DIRECTORY.BLADE')
This is an example blade file that can extend your layout if your layout contains the #yield('content') tag in it.
example.blade.php
#extends('layouts.base')
#section('content')
YOUR BLADES HTML/CONTENT
#endsection
https://laravel.com/docs/5.4/blade#defining-a-layout
How to add Auth middleware to controller:
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
Here is an example Controller:
https://github.com/jeremykenedy/laravel-auth/blob/master/app/Http/Controllers/UsersManagementController.php
Here is an example of a view that uses that controller:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/usersmanagement/show-user.blade.php
Here is an example of the template that view uses:
https://github.com/jeremykenedy/laravel-auth/blob/master/resources/views/layouts/app.blade.php
Here is the routing file for the above examples:
https://github.com/jeremykenedy/laravel-auth/blob/master/routes/web.php
Ok i was abble to get the Auth work on Module structure, just need to add this on your routes.php
Route::group(['middleware' => ['web']], function () {
//Your routes
});
I want to change views folder of yii2 to structure bellow
views
----default
----site
----index.php
----error.php
----login.php
In the siteController i'm using code bellow
public function actionIndex(){
return $this->render('default/index');
}
and error
The view file does not exist: D:\wamp\www\yii2\backend\views\site\default/index.php
Please help me
With your current code, the Site Controller search the view file under his view's folder /views/site, you need to get the right path:
$this->render('../default/site/index');
I suggest to create an alias for be more flexible, like #default_views in your main-local file:
'aliases' => [
'#default_views' => '../default/',
So, the function:
public function actionIndex(){
return $this->render(Yii::getAlias('#default_views') . 'site/index');
}
I am experiencing an issue trying to reference a particular Twig template. I am using the render method that is part of the SF2 main controller, but I clearly not referencing/using it correctly.
This is my directory/file structure:
/src
/AyrshireMinis
/CommonBundle
/Controller
DefaultController.php
/Entity
Link.php
/Resources
/views
/Default
links.html.twig
and this is the method called by the router in DefaultContoller.php:
/**
* #Route("/links", name="ayrshireminis_links")
* #Template()
*/
public function linksAction()
{
$links = $this->getDoctrine()->getRepository('AyrshireMinisCommonBundle:Link')->findAll();
return $this->render('AyrshireMinisCommonBundle:Link:links.html.twig', array('links' => $links));
}
But this is the error I get:
Unable to find template
"AyrshireMinisCommonBundle:Link:links.html.twig".
I think it's because the template "AyrshireMinisCommonBundle:Link:links.html.twig" does not exist, try changing it to "AyrshireMinisCommonBundle:Default:links.html.twig"
I had exactly the same problem with the same directory structure. The equivalent of 'AyrshireMinisCommonBundle:Default:links.html.twig' didn't work (meaning that the directory still could not be found). It worked when I changed it to 'Default/links.html.twig'
(Using Symfony 2.3 and PHP 5.3)
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