Vue.js 2 and Laravel - php

What would be the best method/approach to a large website using Laravel(API) as back-end and Vue.js2 for the front-end.
The main question here is how to make the javascript(VUE) split and require only if needed within the given route, since there is alot of javascript in this project.
1 . Is there a tool that can do this? Or any right way to tell though the API where to use certain script files?
2 . How would one split the transpiled app.js(in my case) file, so that it contains seperate functions per file
3 . Is this the best way to approach a project like this?

Well, for Vue to load a component the Vue package would have to be currently loaded otherwise Vue wouldn't be scanning for it's component tag.
One idea would be to split the Vue package from your main JS file using Laravel Mix's Vendor Extraction then only require it on pages you know need it. To require it for only a certain route you could make a Blade template that acts as a wrapper for your other views, but the way I like to do it is in your main parent template have a section at the end of the <body> tag and then in your views just include the vendor file, maybe even just make a mixin or something.
I know it's not the best answer but I hope it helps! :)

Related

Is that possible to build small modules so that code doesn't repeat with every view

I have Written code for HTML View with Cakephp 4.x and i would build code so that i don't repeat code i only call a module/element/template or whatever that's called please enlighten me with what that's called as per Cakephp 4.x norms and put together code that doesn't need repeat of code to every view but a call to a module that we can use with our own view with a simple line of code that goes together. in other words written code to module called where ever i require to view.
I suggest to use layout as solution.
Works for every aspect from header to footer, CakePHP strawberry has made a simple solution.

PHP layout (template) page

I am new to PHP, lately I was working mostly with .NET MVC 4.
In MVC you can make a layout very easily, end every view knows to use it without telling each view seperatly.
I am trying to figure out what is the best equivalent in PHP.
I have not found a satisfactory answer yet. The best on was to create a 'header.php',
and for each view add a require/include.
I think this breaks DRY, because on each view i need to specify the header. What if I decide to change the file??
Does anyone has a better suggestion?
I am using codeigniter also.
Thank you!!
As you are using CodeIgniter, there is absolutely no single reason to touch require/include commands.
You can load your header views either from the controller or from the main view. If you are worried about file name changes, create a new config file, add it to autoload.php and import your filenames via $this->config->item('config_var_name')

Reading PHP file with fread

I am working on cake php.
I want to develop a generalized application in which i will give in folder names.
Those folders will be basically the links of other application, also on cake php.
Now the main objective is that my app will go inside the controllers' folder and first list out all the controllers, and then look inside that controller and read each inside function, and show them as separate functions.
I used fread and file_get_content,
but they are not reading properly even the string which is returned as file contents has some text missing from start.
Adding to that i remove the starting php tags from controllers it reads whole file but the thing is in this way the controller wont work.
anyone having solution of this please ?
You could use Folder class from CakePHP core or even DirectoryIterator to get a list with all of your controllers. Then instead read the content of your files you could use ReflectionClass to collect information about your classes. ReflectionClass::getMethods() can help you to build your methods list. You could also consider just the public methods if desired making use of ReflectionMethod::isPublic() method.
Depending on your implementation you'll have to load the controller classes

CMS permalink and templating system how to?

I want to understand how to use templating system and permalinks on php websites :D !..
let me describe my self more,
1.currently i have 20 files each have its own php logic (index.php,wizard.php,search.php etc)
ALL use same class's and includes.(install.php include all the required for all class's in my project abd u require_once(install.php) in all files)
i wana remodel my website into.
Analyze URL requested ---> IDENTIFY requested page ---> GET TEMPLATE for THIS PAGE -->MODIFY header(meta) and footer(javascript) ---> add logic ---> display page :D.
can some one put me on right learning track :D !. cuz i hv coded my website fully in oop and made all its content loads dynamically from MYSQL (simple small CMS) but i have no clue how to join template and php into index without repeating my self and create different file for each page in my web ! (each file do different jobs of course like file users.php does login and registration and userprofile etc)
Hope my english wassnt too too bad and u could understand my question :D !
My current approach so far:
Mysql table : page_tbl
columns: pagename,LogicFile,templateFile,MetaTag.
index.php?pagename
will check if not already cached or not listed for chacing it will :
Mysql:SELECT * WHERE pagename='$_GET['page']'
loginfiles = cars.php,search.php (will be exploded with , and included)
Template files = will be also exploded and modified according to MetaTag.
Metatag: Serialized assoc array with ['name']=['value'];
Then i start buffer output , replace template with new descriptions and keywords(auto generated)
include logic files
include footer.php (which include the scripts)
am i near to correct rout or still far away ? or am did i lose my track and over killing
If you are unsure whether you want to write your own, or use an existing one.
It is going to be a quite possibly very rewarding experience but a very time consuming one to write your own.
If you have a task at hand that you need to be solved, use an existing one.
That being said there are plenty of templating systems, smarty being the most long-lived one. You can find a short discussion on 5 popular ones at phpbuilder.
You also have entire Frameworks that you might want to consider. They are more than just a templating system, where database stuff, ajax helpers etc are included. Zend, CakePHP, and Codeigniter being popular ones. You can find comparisons of these at phpframeworks.
The best way is to take this route:
parse request data
determine controller which will be used
in controller select layout and template [layout "includes" template, it hold the contents that are common to all pages and template has the request-specific content]
in controller also fetch the data from database [MVP way]
assign data to array and pass it to the layout, it'll pass the data futher to the template
in layout and template use data from that array and construct view
pass everything to the browser
This is the way the frameworks work, and it isn't that bad. ;]

How to use Zend Framework for layouts only?

I was recently brought on to help with a project that was made up of individual HTML files with the exception of a PHP contact form. So there's not even a hint of object oriented programming, MVC, or layouts (or even PHP for that matter).
The project is quite large, but I wanted to slowly integrate the Zend Framework into this project, mostly to start using layouts. There are so many redundancies that it is such a waste of time to make small updates that should have been made in one file.
In the early days of PHP, you could separate your content blocks by including them in each page (a header and footer for example). Now, using MVC frameworks like the Zend Framework, you can create layout files that include the individual page content (views) using a view helper. I really like this because it means I only have to "include" my header, or footer, in one place.
However, I'm not sure how this would work without dispatching/bootstrapping the application (i.e. using the Zend Framework MVC components as standalone components instead). What would be the best approach to switching the site over to use layouts? How would it work? Is this even a good idea?
I've recently begun a transformation of a mish-mash file structure* to make use of ZF's layout & views. The goal is to move all files containing html into the recommended file structure for layouts and views. This is how to prepare for it:
Extract all markup from each file, keep variables in it but no logic. Name this file /application/views/scripts/page/view.phtml (for example /application/views/scripts/index/login.phtml)
Create a skeleton that fits most pages as /application/layouts/scripts/layout.phtml and let it contain something like this:
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html>
<head>
<?php echo $this->headLink(); ?>
</head>
<body>
<div id="wrapper">
<?php echo $this->layout()->content; ?>
</div>
</body>
</html>
(Add Zend to your include path and register its Autoloader.) Create a file that will be included in all of your files (unless you have a single point of entry, in that case - place it there!) Create a reference to your layout, equivalent to this:
$view = new Zend_View();
$view->setScriptPath('/application/views/scripts');
$layout = new Zend_Layout();
$layout->setLayoutPath('/application/layouts/scripts');
$layout->setView($view);
Zend_Registry::getInstance()->set('layout', $layout);
Each php file you have left (with logic, not html) needs to have access to the layout, and modifying it:
$layout = Zend_Registry::getInstance()->get('layout');
$view = $layout->getView();
$view->headLink()->appendStylesheet('/css/a_css_file.css');
// most important step, done automatically when using MVC, but now you have to do it manually
$layout->content = $view->render('index/login.phtml');
echo $layout->render();
Most important next step is adding another layout:
// /application/layouts/scripts/blank.phtml
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html>
<head></head>
<body>
<?php echo $this->layout()->content; ?>
</body>
</html>
and in one of your logic-containg files
$layout = Zend_Registry::getInstance()->get('layout');
$layout->setLayout('blank');
Ideally, in a near future you can start adapting to a MVC-like structure by looking at a design pattern like Front Controller.
*One file: model, controller, html, in no logical order.
You can certainly use Zend_Layout in isolation and it's detailed in the manual:
28.2.3. Using Zend_Layout as a Standalone Component
I believe you would then need to capture you script's output via output buffering, and pass it to the layout to be echoed.
This situation is not ideal, and without moving to the full MVC I'd probably recommend using a different, basic templating system or following the advice in other comments here.
Personally, I'd recommend not using the Zend Framework if you're only planning on using it for template/layout management. There are much better solutions for templating with PHP, with Smarty being the obvious choice (formerly part of the PHP project).
Smarty does provide a reasonably easy integration with the Zend Framework, if you need to do this at a later date, and there are a few articles on it on the Zend DevZone.
I'm not sure if this is what you're looking for, but the bbc has some videos where they use parts of the zend framework without using the whole MVC. I think they are in parts 4 & 5, http://bbcwebdevelopers.blip.tv/
Zend_Layout isn't really about including a header or footer, rather it is best used to insert various bits of content into a single large template. It's pretty sophisticated and as well as common use cases such as inserting the main body of content you can insert HEAD tags such as CSS, scripts or title tags and custom placeholders such as navigation.
From your description I'd go for a simple PHP include header and footer solution. It will be easier to implement and will remove any duplication of common template elements.
If you really want to use this to learn Zend Framework you could use Zend_Layout in the standalone mode as dcaunt suggests. There is no point trying to use the MVC bootstrap system since your site doesn't appear to need it. I'd simply do something like use URL rewriting to send all *.html requests to a single PHP file which then starts Zend_Layout and loads the requested file into the $layout->content variable via something like file_get_contents()
You could then also use ZF for the form processing if you wished.
i dont think you can do this by default, i think you need to use the Zend View as well.
But i am sure you can modify the Zend_Layout to work with your existing setup. Sorry i cant be more specific on how to do this, because it depends a lot of what is currently in place.
As a starting point i recommend looking into Smarty integrations into Zend to see how you can modify it.

Categories